{"id":25849556,"url":"https://github.com/fjcaetano/react-hook-utilities","last_synced_at":"2025-03-01T11:29:28.714Z","repository":{"id":35122967,"uuid":"209412379","full_name":"fjcaetano/react-hook-utilities","owner":"fjcaetano","description":"⥿ Utilitarian React hooks","archived":false,"fork":false,"pushed_at":"2023-01-04T10:44:14.000Z","size":1944,"stargazers_count":6,"open_issues_count":17,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-18T20:49:29.634Z","etag":null,"topics":["async-effects","helpers","promise","react","react-hooks","utilities","utils"],"latest_commit_sha":null,"homepage":"https://fjcaetano.github.com/react-hook-utilities","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/fjcaetano.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2019-09-18T22:09:29.000Z","updated_at":"2022-02-14T22:42:51.000Z","dependencies_parsed_at":"2023-01-15T14:30:24.220Z","dependency_job_id":null,"html_url":"https://github.com/fjcaetano/react-hook-utilities","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fjcaetano%2Freact-hook-utilities","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fjcaetano%2Freact-hook-utilities/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fjcaetano%2Freact-hook-utilities/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fjcaetano%2Freact-hook-utilities/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fjcaetano","download_url":"https://codeload.github.com/fjcaetano/react-hook-utilities/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241359132,"owners_count":19950041,"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":["async-effects","helpers","promise","react","react-hooks","utilities","utils"],"created_at":"2025-03-01T11:28:55.624Z","updated_at":"2025-03-01T11:29:27.986Z","avatar_url":"https://github.com/fjcaetano.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"react-hook-utilities [![Build Status][1]](https://github.com/fjcaetano/react-hook-utilities/actions) [![codecov][2]](https://codecov.io/gh/fjcaetano/react-hook-utilities) [![PRs welcome][4]](https://github.com/fjcaetano/react-hook-utilities/pulls) [![npm][3]](https://www.npmjs.com/package/react-hook-utilities)\n---\n\nA set of extraordinarily common React hooks.\n\n# Installation\n\nWe recommend installing using `yarn`:\n```sh\n$ yarn add react-hook-utilities\n```\n\nOr, if you prefer `npm`:\n```sh\n$ npm -S react-hook-utilities\n```\n\n# Usage\n\n### Async effects\n\nRun asynchronous effects:\n\n```ts\nuseAsyncEffect(async () =\u003e {\n  await promise;\n}, []);\n```\n\n### Async layout effects\n\nRun asynchronous layout effects:\n\n```ts\nuseAsyncLayoutEffect(async () =\u003e {\n  await promise;\n}, []);\n```\n\n### Effect update\n\nExecutes an effect and get its dependencies previous state:\n\n```ts\nlet name: string | undefined;\n\nuseEffectUpdate(\n  ([oldName]) =\u003e {\n    if (!oldName \u0026\u0026 !!name) {\n      // name now has a valid value\n    }\n  },\n  [name],\n)\n```\n\n### Conditional effect\n\nConditionally executes an effect. The previous state is sent to the condition evaluation:\n\n```ts\nlet error: Error | undefined;\n\nuseConditionalEffect(\n  ([prevError]) =\u003e !prevError \u0026\u0026 !!error,\n  () =\u003e {\n    // an error was introduced\n    showToast(error);\n  },\n  [error],\n)\n```\n\n### Worker\n\nWraps a promise with loading and error states:\n\n```ts\nconst { isLoading, error, result, callback } = useWorker(\n  async param =\u003e {\n    if (!param) {\n      throw new Error('invalid arguments');\n    }\n\n    const result = await doSomething(param)\n    return result\n  },\n  [doSomething],\n)\n```\n\n`isLoading` is set to true as soon as the callback is loaded and only returns to `false` when it\nends or when an error happens. If an exception is thrown or a promise fails, `error` will be updated.\n\n### Worker State\n\nThis is a worker that starts loading immediately and stores the result in a state. Useful for\nloading data when you render a component:\n\n```ts\nconst { isLoading, error, data, callback } = useWorkerState(\n  async () =\u003e {\n    return await getUserName(userId);\n  },\n  [userId],\n  'no-name', // data's initial value\n);\n```\n\n### Did Mount\n\nRuns an effect when the component mounts:\n\n```ts\nuseDidMount(() =\u003e {\n  console.log(\"I'm up!\");\n});\n```\n\nThe effect may be an asynchronous function, in which case, it shouldn't return a cleanup function\nsince it won't be executed.\n\n### Did Unmount\n\nExecutes an effect when the component unmounts. The effect may also be asynchronous:\n\n```ts\nuseDidUnmount(async () =\u003e {\n  await busyWork(someState);\n  console.log('heading out');\n}, [someState]);\n```\n\nAny dependencies used inside the effect must be declared, however, the effect is not called when the\ndependencies change. The effect is only called when the component is being unmounted.\n\n### Use Lazy Ref\n\nCreates a mutable reference object from a factory function.\n\n```ts\nconst ref = useLazyRef(() =\u003e new SomeObject());\n...\nref.current = newObject;\n```\n\n### Use Promised State\n\nA state that only resolves after setting truthy values.\n\n If you need to use the promise as a dependency of another hook, use its [ValuablePromise.value](https://github.com/fjcaetano/react-hook-utilities/blob/master/src/index.tsx#L52) attribute as the dependency:\n ``` ts\n const [name, setName, rejectName] = usePromisedState();\n useAsyncEffect(async () =\u003e {\n   await name;\n   ...\n }, [name.value]); // use the promise's `value` as dependency\n\n const someCallback = useCallback(async () =\u003e {\n   try {\n    const name = await fetch(API.getName);\n    setName(name);\n   } catch(e) {\n     rejectName(e);\n   }\n }, []);\n ```\n\n\n# Typescript\n\nreact-hook-utilities sees Typescript is a first-class citizen. The library is built for and around Typescript and you'll get bonus points for using it. Nonetheless, pure JavaScript files are also available if you're _that_ guy.\n\n# [Full Documentation](https://fjcaetano.github.com/react-hook-utilities)\n\n[1]: https://github.com/fjcaetano/react-hook-utilities/workflows/Node%20CI/badge.svg\n[2]: https://codecov.io/gh/fjcaetano/react-hook-utilities/branch/master/graph/badge.svg\n[3]: https://img.shields.io/npm/v/react-hook-utilities\n[4]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffjcaetano%2Freact-hook-utilities","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffjcaetano%2Freact-hook-utilities","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffjcaetano%2Freact-hook-utilities/lists"}