{"id":18744500,"url":"https://github.com/ifedapoolarewaju/use-clean-effect","last_synced_at":"2025-04-12T21:32:34.371Z","repository":{"id":40520498,"uuid":"505176248","full_name":"ifedapoolarewaju/use-clean-effect","owner":"ifedapoolarewaju","description":"An extension of React useEffect without the need for clean-up functions","archived":false,"fork":false,"pushed_at":"2022-06-27T19:10:12.000Z","size":53,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-03T21:19:23.668Z","etag":null,"topics":["npm-package","react","react-hook","react-hooks","reactjs","use-clean-effect","use-effect","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/ifedapoolarewaju.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}},"created_at":"2022-06-19T17:29:35.000Z","updated_at":"2022-06-30T18:53:25.000Z","dependencies_parsed_at":"2022-07-31T23:48:42.606Z","dependency_job_id":null,"html_url":"https://github.com/ifedapoolarewaju/use-clean-effect","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ifedapoolarewaju%2Fuse-clean-effect","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ifedapoolarewaju%2Fuse-clean-effect/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ifedapoolarewaju%2Fuse-clean-effect/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ifedapoolarewaju%2Fuse-clean-effect/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ifedapoolarewaju","download_url":"https://codeload.github.com/ifedapoolarewaju/use-clean-effect/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248636188,"owners_count":21137396,"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":["npm-package","react","react-hook","react-hooks","reactjs","use-clean-effect","use-effect","useeffect","useeffect-hook","useeffects"],"created_at":"2024-11-07T16:15:02.318Z","updated_at":"2025-04-12T21:32:34.069Z","avatar_url":"https://github.com/ifedapoolarewaju.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# use-clean-effect\n\nAn extended version of React useEffect that is built-in with a simple clean-up function, hence, you may not need to add your own clean-up function when using this.\n\nIt also passes an argument to the useEffect callback that indicates if the component has been unmounted or re-rendered. Therefore, making it easy to invoke async functions in your useEffect callback and avoid memory leaks and removing the need for you to add a clean-up function.\n\n## Installation\n\n```sh\nnpm install use-clean-effect\n```\n\nor\n\n```sh\nyarn add use-clean-effect\n```\n\n## Usage\n\n`useCleanEffect` strives to preserve the usage interface of React's `useEffect`.\nYou can essentially swap `useCleanEffect` with  React's `useEffect` without any change in your codebase.\n\nHowever, if you want to leverage on the extra feature offered by `useCleanEffect` you can access the extra argument passed to the `useCleanEffect` callback like so:\n\n```js\nimport { useCleanEffect } from 'use-clean-effect'\n\nuseCleanEffect((phase) =\u003e {\n    // an asynchronous call\n    someHttpRequest().then((data) =\u003e {\n        if (!phase.active) {\n            // component has been unmounted/re-rendered so we abort to avoid memory-leak\n            return\n        }\n\n        // go ahead to use data since then component has not been unmounted/re-rendered\n        ...\n    })\n}, [])\n```\n\nThe `phase` argument is an object that contains a boolean field `active` which has the value `true` if the component hasn't been unmounted or re-rendered since the `useEffect` callback was triggered. And is `false` otherwise (in which case, we should abort the function to avoid memory leak).\n\n### Additional clean-up Function\n\nFor most cases you won't need to add a clean up function to your `useCleanEffect` callback, since it implicitly handles the clean-up logic. However, if your use-case requires that you specifically handle a clean-up logic, you can still return your clean-up function the way you would do it for React's `useEffect`.\n\n```js\nimport { useCleanEffect } from 'use-clean-effect'\n\nuseCleanEffect((phase) =\u003e {\n    // an async call\n    someHttpRequest().then((data) =\u003e {\n        // ...\n    })\n\n    const customCleanUpFunction = () =\u003e {\n        // custom clean-up logic here\n    }\n\n    return customCleanUpFunction\n}, [])\n```\n\nIf you would like to swap all occurrences of React's `useEffect` with `useCleanEffect` without much modification, you can simply import `useCleanEffect` using the `as` keyword like so:\n\n```js\nimport { useCleanEffect as useEffect } from 'use-clean-effect'\n// and then delete occurrences of `import { useEffect } from 'react'`\n```\n\n## License\n\n[The MIT License](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fifedapoolarewaju%2Fuse-clean-effect","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fifedapoolarewaju%2Fuse-clean-effect","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fifedapoolarewaju%2Fuse-clean-effect/lists"}