{"id":17182348,"url":"https://github.com/davidhu2000/use-better-effect","last_synced_at":"2026-01-23T19:31:35.569Z","repository":{"id":66101795,"uuid":"577628230","full_name":"davidhu2000/use-better-effect","owner":"davidhu2000","description":"A wrapper around `React.useEffect` but with improved API","archived":false,"fork":false,"pushed_at":"2022-12-15T23:24:07.000Z","size":37274,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-11-04T17:20:37.207Z","etag":null,"topics":["react","react-hooks","react-native","reactjs","useeffect","useeffect-hook","useeffects"],"latest_commit_sha":null,"homepage":"","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/davidhu2000.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"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":"2022-12-13T06:55:08.000Z","updated_at":"2025-01-06T10:47:32.000Z","dependencies_parsed_at":"2023-03-03T14:30:55.533Z","dependency_job_id":null,"html_url":"https://github.com/davidhu2000/use-better-effect","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/davidhu2000/use-better-effect","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidhu2000%2Fuse-better-effect","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidhu2000%2Fuse-better-effect/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidhu2000%2Fuse-better-effect/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidhu2000%2Fuse-better-effect/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/davidhu2000","download_url":"https://codeload.github.com/davidhu2000/use-better-effect/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidhu2000%2Fuse-better-effect/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28698842,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-23T17:25:48.045Z","status":"ssl_error","status_checked_at":"2026-01-23T17:25:47.153Z","response_time":59,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["react","react-hooks","react-native","reactjs","useeffect","useeffect-hook","useeffects"],"created_at":"2024-10-15T00:36:51.214Z","updated_at":"2026-01-23T19:31:35.321Z","avatar_url":"https://github.com/davidhu2000.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# useBetterEffect\n\n[![npm version](https://img.shields.io/npm/v/use-better-effect.svg)][npm_url]\n[![downloads](https://img.shields.io/npm/dt/use-better-effect.svg)][npm_url]\n![license](https://img.shields.io/npm/l/use-better-effect.svg)\n![size](https://img.shields.io/bundlephobia/minzip/use-better-effect)\n\n[![Coverage Status](https://coveralls.io/repos/github/davidhu2000/use-better-effect/badge.svg?branch=main)](https://coveralls.io/github/davidhu2000/use-better-effect?branch=main)\n![Dependency Count](https://badgen.net/bundlephobia/dependency-count/use-better-effect)\n![Types Included](https://img.shields.io/npm/types/use-better-effect)\n![Tree Shaking Supported](https://badgen.net/bundlephobia/tree-shaking/use-better-effect)\n\n[npm_url]: https://www.npmjs.org/package/use-better-effect\n\nA wrapper around `React.useEffect` but with improved API\n\n## Installation\n\nWith Yarn:\n\n```bash\nyarn add use-better-effect\n```\n\nWith npm:\n\n```bash\nnpm install --save use-better-effect\n```\n\n## Background\n\n`useEffect` is a powerful tool but the API has some gotchas. See the following examples:\n\n```ts\n// someFn runs on every render\nuseEffect(() =\u003e {\n  someFn();\n});\n\n// someFn only run on mount\nuseEffect(() =\u003e {\n  someFn();\n}, []);\n\n// someFn rerun when a or b changes\nuseEffect(() =\u003e {\n  someFn();\n}, [a, b]);\n\n// the returned function is called on unmount\nuseEffect(() =\u003e {\n  someFn();\n  return () =\u003e anotherFn();\n}, [a, b]);\n```\n\nThese implicit behaviors are hard to understand by just looking at the code.\n\n## Improved API\n\n`useBetterEffect` uses typescript function overloading to improve the developer experience when using the effect for different conditions. There are 3 supports run conditions:\n\n- ON_MOUNT\n- EVERY_RENDER\n- DEPENDENCIES_CHANGED\n\nFor `ON_MOUNT` and `EVERY_RENDER`, passing in dependencies will result in a type error and `useBetterEffect` auto computes the dependency argument as `[]` and `undefined` respectively.\n\nThe cleanup function is passing as an explict optional arugment.\n\n## Examples\n\n```ts\nimport { useBetterEffect } from \"use-better-effect\";\n\nuseBetterEffect({\n  callbackFn: () =\u003e console.log(\"yay better\"),\n  cleanupFn: () =\u003e console.log(\"so fresh and so clean\"),\n  runCondition: \"ON_MOUNT\",\n});\n\nuseBetterEffect({\n  callbackFn: () =\u003e console.log(\"yay better\"),\n  cleanupFn: () =\u003e console.log(\"so fresh and so clean\"),\n  runCondition: \"EVERY_RENDER\",\n});\n\nuseBetterEffect({\n  callbackFn: () =\u003e console.log(\"yay better\"),\n  cleanupFn: () =\u003e console.log(\"so fresh and so clean\"),\n  runCondition: \"DEPENDENCIES_CHANGED\",\n  dependencies: [count],\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidhu2000%2Fuse-better-effect","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdavidhu2000%2Fuse-better-effect","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidhu2000%2Fuse-better-effect/lists"}