{"id":15493245,"url":"https://github.com/kotarella1110/use-custom-compare","last_synced_at":"2025-04-09T19:18:57.286Z","repository":{"id":37092167,"uuid":"244304125","full_name":"kotarella1110/use-custom-compare","owner":"kotarella1110","description":"It's React's useEffect/useMemo/useCallback hooks, except using custom comparison on the inputs, not reference equality","archived":false,"fork":false,"pushed_at":"2025-03-31T12:08:51.000Z","size":4018,"stargazers_count":86,"open_issues_count":29,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-09T19:18:51.557Z","etag":null,"topics":["hooks","react","react-hooks","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/use-custom-compare","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kotarella1110.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-03-02T07:10:41.000Z","updated_at":"2025-04-03T03:26:14.000Z","dependencies_parsed_at":"2024-06-18T18:39:08.889Z","dependency_job_id":null,"html_url":"https://github.com/kotarella1110/use-custom-compare","commit_stats":{"total_commits":535,"total_committers":4,"mean_commits":133.75,"dds":0.09345794392523366,"last_synced_commit":"27003b13804b7dd53350463ee4c0dd9756aa2e92"},"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kotarella1110%2Fuse-custom-compare","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kotarella1110%2Fuse-custom-compare/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kotarella1110%2Fuse-custom-compare/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kotarella1110%2Fuse-custom-compare/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kotarella1110","download_url":"https://codeload.github.com/kotarella1110/use-custom-compare/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248094990,"owners_count":21046770,"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":["hooks","react","react-hooks","typescript"],"created_at":"2024-10-02T08:05:09.826Z","updated_at":"2025-04-09T19:18:57.256Z","avatar_url":"https://github.com/kotarella1110.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\n\u003ch1\u003euse-custom-compare\u003c/h1\u003e\n\nIt's React's useEffect/useMemo/useCallback hooks, except using custom comparison on the inputs, not reference equality\n\n[![License](https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square)](LICENSE)\n[![Actions Status](https://github.com/kotarella1110/use-custom-compare/workflows/CI/badge.svg)](https://github.com/kotarella1110/use-custom-compare/actions?query=workflow%3ACI)\n[![NPM Version](https://img.shields.io/npm/v/use-custom-compare?style=flat-square)](https://www.npmjs.com/package/use-custom-compare)\n[![Downloads Month](https://img.shields.io/npm/dm/use-custom-compare?style=flat-square)](https://www.npmjs.com/package/use-custom-compare)\n[![Downloads Total](https://img.shields.io/npm/dt/use-custom-compare?style=flat-square)](https://www.npmjs.com/package/use-custom-compare)\n[![Dependencies Status](https://david-dm.org/kotarella1110/use-custom-compare.svg?style=flat-square)](https://david-dm.org/kotarella1110/use-custom-compare)\n[![Semantic Release](https://img.shields.io/badge/%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=flat-square)](https://github.com/semantic-release/semantic-release)\n[![Commitizen Friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?style=flat-square)](http://commitizen.github.io/cz-cli/)\n[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-green.svg?style=flat-square)](CONTRIBUTING.md)\n\n\u003c!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section --\u003e\n\n[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors-)\n\n\u003c!-- ALL-CONTRIBUTORS-BADGE:END --\u003e\n\n\u003c/div\u003e\n\n## Installation\n\n```\nnpm install use-custom-compare\n\n# or\n\nyarn add use-custom-compare\n```\n\n## Usage\n\n### useCustomCompareEffect\n\n```js\nimport React from \"react\";\nimport { useCustomCompareEffect } from \"use-custom-compare\";\nimport isEqual from \"lodash/isEqual\";\n\nfunction App({ options }) {\n  useCustomCompareEffect(\n    () =\u003e {\n      // do something significant here\n      return () =\u003e {\n        // return to clean up that significant thing\n      };\n    },\n    [options],\n    (prevDeps, nextDeps) =\u003e isEqual(prevDeps, nextDeps)\n  );\n\n  return \u003cdiv\u003e{/* render significant thing */}\u003c/div\u003e;\n}\n```\n\n### useCustomCompareLayoutEffect\n\n```js\nimport React from \"react\";\nimport { useCustomCompareLayoutEffect } from \"use-custom-compare\";\nimport isEqual from \"lodash/isEqual\";\n\nfunction App({ options }) {\n  useCustomCompareLayoutEffect(\n    () =\u003e {\n      // do something significant here\n      return () =\u003e {\n        // return to clean up that significant thing\n      };\n    },\n    [options],\n    (prevDeps, nextDeps) =\u003e isEqual(prevDeps, nextDeps)\n  );\n\n  return \u003cdiv\u003e{/* render significant thing */}\u003c/div\u003e;\n}\n```\n\n### useCustomCompareMemo\n\n```js\nimport React from \"react\";\nimport { useCustomCompareMemo } from \"use-custom-compare\";\nimport isEqual from \"lodash/isEqual\";\n\nfunction App({ options }) {\n  const memoized = useCustomCompareMemo(\n    () =\u003e {\n      // do something significant here\n    },\n    [options],\n    (prevDeps, nextDeps) =\u003e isEqual(prevDeps, nextDeps)\n  );\n\n  return \u003cdiv\u003e{/* render significant thing */}\u003c/div\u003e;\n}\n```\n\n### useCustomCompareCallback\n\n```js\nimport React from \"react\";\nimport { useCustomCompareCallback } from \"use-custom-compare\";\nimport isEqual from \"lodash/isEqual\";\n\nfunction App({ options }) {\n  const memoized = useCustomCompareCallback(\n    () =\u003e {\n      // do something significant here\n    },\n    [options],\n    (prevDeps, nextDeps) =\u003e isEqual(prevDeps, nextDeps)\n  );\n\n  return \u003cdiv\u003e{/* render significant thing */}\u003c/div\u003e;\n}\n```\n\n## TypeScript\n\nThis custom compare hooks is type-safe because it is built with TypeScript, which requires the use of TypeScript 4.0 or higher.\n\n```tsx\nimport React from \"react\";\nimport { useCustomCompareEffect } from \"use-custom-compare\";\nimport isEqual from \"lodash/isEqual\";\n\nfunction App() {\n  useCustomCompareEffect(\n    () =\u003e {},\n    [1, { a: \"b\" }, true],\n    (\n      prevDeps, // type: readonly [number, { a: string }, boolean]\n      nextDeps // type: readonly [number, { a: string }, boolean]\n    ) =\u003e isEqual(prevDeps, nextDeps)\n  );\n\n  return \u003cdiv /\u003e;\n}\n```\n\n## ESLint\n\n`exhaustive-deps` in [eslint-plugin-react-hooks](https://www.npmjs.com/package/eslint-plugin-react-hooks) can be configured to validate dependencies.\nIf you want to apply that rule to this custom compare hooks as well, use the [`additionalHooks` option](https://www.npmjs.com/package/eslint-plugin-react-hooks#advanced-configuration).\n\n```js\n{\n  \"rules\": {\n    // ...\n    \"react-hooks/exhaustive-deps\": [\n      \"warn\",\n      {\n        additionalHooks:\n          \"(useCustomCompareEffect|useCustomCompareLayoutEffect|useCustomCompareMemo|useCustomCompareCallback)\"\n      }\n    ]\n  }\n}\n```\n\n## Note\n\nIn the following cases, use React's useEffect/useMemo/useCallback hooks instead of this custom compare hooks!\n\n- no dependencies\n- dependencies are all primitive values\n\n## Contributing\n\nContributions are always welcome! Please read the [contributing](./CONTRIBUTING.md) first.\n\n## Inspiration\n\n- [`use-deep-compare-effect`](https://github.com/kentcdodds/use-deep-compare-effect) 🐋 It's react's useEffect hook, except using deep comparison on the inputs, not reference equality.\n- [`use-deep-compare`](https://github.com/sandiiarov/use-deep-compare) It's react's useEffect/useMemo/useCallback hooks, except using deep comparison on the inputs.\n- [`use-custom-compare-effect`](https://github.com/sanjagh/use-custom-compare-effect) useEffect hook which takes a comparison function instead of compare using reference equality.\n\n## Contributors\n\nThanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --\u003e\n\u003c!-- prettier-ignore-start --\u003e\n\u003c!-- markdownlint-disable --\u003e\n\u003ctable\u003e\n  \u003ctr\u003e\n    \u003ctd align=\"center\"\u003e\u003ca href=\"https://qiita.com/kotarella1110\"\u003e\u003cimg src=\"https://avatars1.githubusercontent.com/u/12913947?v=4\" width=\"100px;\" alt=\"\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eKotaro Sugawara\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/kotarella1110/use-custom-compare/commits?author=kotarella1110\" title=\"Code\"\u003e💻\u003c/a\u003e \u003ca href=\"https://github.com/kotarella1110/use-custom-compare/commits?author=kotarella1110\" title=\"Documentation\"\u003e📖\u003c/a\u003e \u003ca href=\"#ideas-kotarella1110\" title=\"Ideas, Planning, \u0026 Feedback\"\u003e🤔\u003c/a\u003e \u003ca href=\"#infra-kotarella1110\" title=\"Infrastructure (Hosting, Build-Tools, etc)\"\u003e🚇\u003c/a\u003e \u003ca href=\"https://github.com/kotarella1110/use-custom-compare/commits?author=kotarella1110\" title=\"Tests\"\u003e⚠️\u003c/a\u003e\u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\n\u003c!-- markdownlint-enable --\u003e\n\u003c!-- prettier-ignore-end --\u003e\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:END --\u003e\n\nThis project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!\n\n## License\n\n[MIT](./LICENSE) © [Kotaro Sugawara](https://twitter.com/kotarella1110)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkotarella1110%2Fuse-custom-compare","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkotarella1110%2Fuse-custom-compare","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkotarella1110%2Fuse-custom-compare/lists"}