{"id":23186082,"url":"https://github.com/termosa/use-patch","last_synced_at":"2025-04-05T04:44:59.188Z","repository":{"id":57388263,"uuid":"423105068","full_name":"termosa/use-patch","owner":"termosa","description":"Track diff when changing objects","archived":false,"fork":false,"pushed_at":"2023-08-11T01:03:49.000Z","size":531,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-18T15:47:20.767Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/termosa.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-10-31T09:33:32.000Z","updated_at":"2021-10-31T09:33:47.000Z","dependencies_parsed_at":"2022-09-05T03:20:33.461Z","dependency_job_id":null,"html_url":"https://github.com/termosa/use-patch","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/termosa%2Fuse-patch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/termosa%2Fuse-patch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/termosa%2Fuse-patch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/termosa%2Fuse-patch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/termosa","download_url":"https://codeload.github.com/termosa/use-patch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247289400,"owners_count":20914464,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-18T10:14:26.028Z","updated_at":"2025-04-05T04:44:59.167Z","avatar_url":"https://github.com/termosa.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# use-patch\n\n\u003e Track diff when changing objects\n\n[![NPM](https://img.shields.io/npm/v/use-patch.svg)](https://www.npmjs.com/package/use-patch) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)\n\n## Install\n\n```bash\nnpm install --save use-patch\n```\n\n## Usage\n\n```tsx\nimport * as React from 'react'\n\nimport usePatch from 'use-patch'\n// Or: import { usePatch } from 'use-patch'\n\nconst Example = () =\u003e {\n  const {\n    diff,    // The list of properties that are different from the origin object\n    value,   // The origin object with patches applied to it\n    changed, // Boolean value that shows if diff contains changes\n    apply,   // Function that adds changes to the origin object\n    set,     // Function that defines the resulting object (the diff will be calculated from the origin and given object)\n    reset,   // Function that resets all changes\n  } = usePatch(\n    origin, // The origin object to be compared with stored changes\n  );\n\n  // ...\n};\n```\n\n## Examples\n\n### Using it to track changed properties of the profile\n\n[Source code](https://github.com/termosa/use-patch/blob/master/example/src/ProfileEditingExample.js)\n\n```tsx\nconst ProfileEditingExample = () =\u003e {\n  const profile = { firstName: \"Jeff\", lastName: \"Adams\" };\n  const profilePatch = usePatch(profile);\n\n  return (\n    \u003cdiv\u003e\n      \u003ch3\u003eOrigin\u003c/h3\u003e\n      \u003cpre\u003e{JSON.stringify(profile, null, 2)}\u003c/pre\u003e\n      \u003cform\u003e\n        \u003ch3\u003eEditor\u003c/h3\u003e\n        \u003cdiv\u003e\n          \u003clabel\u003e\n            First name\n            \u003cinput\n              value={profilePatch.value.firstName}\n              onChange={(e) =\u003e\n                profilePatch.apply({ firstName: e.target.value })\n              }\n            /\u003e\n          \u003c/label\u003e\n        \u003c/div\u003e\n        \u003cdiv\u003e\n          \u003clabel\u003e\n            Last name\n            \u003cinput\n              value={profilePatch.value.lastName}\n              onChange={(e) =\u003e profilePatch.apply({ lastName: e.target.value })}\n            /\u003e\n          \u003c/label\u003e\n        \u003c/div\u003e\n        \u003cdiv\u003e\n          \u003clabel\u003e\n            Age\n            \u003cinput\n              value={profilePatch.value.age || \"\"}\n              onChange={(e) =\u003e\n                profilePatch.apply({ age: +e.target.value || undefined })\n              }\n            /\u003e\n          \u003c/label\u003e\n        \u003c/div\u003e\n      \u003c/form\u003e\n      \u003ch3\u003eThe diff\u003c/h3\u003e\n      \u003cpre\u003e{JSON.stringify(profilePatch.diff, null, 2)}\u003c/pre\u003e\n    \u003c/div\u003e\n  );\n};\n```\n\n### Example of collecting data for PUT request\n\n```tsx\nimport api from './api';\n\nconst ProfilePage = ({ profile }) =\u003e {\n  const profilePatch = usePatch(profile);\n\n  return (\n    \u003cProfileForm\n      defaultValue={profile}\n      onProfileChange={profilePatch.set}\n      onSubmit={() =\u003e api.put(profilePatch.diff)}\n    /\u003e\n  );\n};\n```\n\n## License\n\nMIT © [termosa](https://github.com/termosa)\n\n---\n\nThis hook is created using [create-react-hook](https://github.com/hermanya/create-react-hook).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftermosa%2Fuse-patch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftermosa%2Fuse-patch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftermosa%2Fuse-patch/lists"}