{"id":13727003,"url":"https://github.com/feerzlay/react-use-resource","last_synced_at":"2025-05-07T22:30:44.617Z","repository":{"id":57347185,"uuid":"297586382","full_name":"feerzlay/react-use-resource","owner":"feerzlay","description":"Convert a promise returning function into a suspense compatible resource.","archived":false,"fork":false,"pushed_at":"2020-12-27T17:01:52.000Z","size":366,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-20T14:16:25.458Z","etag":null,"topics":["data-fetching","hooks","react","suspense"],"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/feerzlay.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":"2020-09-22T08:35:09.000Z","updated_at":"2021-03-26T04:18:49.000Z","dependencies_parsed_at":"2022-09-18T11:03:20.610Z","dependency_job_id":null,"html_url":"https://github.com/feerzlay/react-use-resource","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/feerzlay%2Freact-use-resource","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/feerzlay%2Freact-use-resource/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/feerzlay%2Freact-use-resource/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/feerzlay%2Freact-use-resource/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/feerzlay","download_url":"https://codeload.github.com/feerzlay/react-use-resource/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252965166,"owners_count":21832831,"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":["data-fetching","hooks","react","suspense"],"created_at":"2024-08-03T01:03:35.217Z","updated_at":"2025-05-07T22:30:43.917Z","avatar_url":"https://github.com/feerzlay.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003ereact-use-resource\u003c/h1\u003e\n\nConvert a promise returning function into a suspense compatible resource.\n\n[![license](https://img.shields.io/github/license/feerzlay/react-use-resource)](https://github.com/feerzlay/react-use-resource/blob/master/LICENSE)\n[![npm](https://img.shields.io/npm/v/react-use-resource)](https://www.npmjs.com/package/react-use-resource)\n[![npm downloads](https://img.shields.io/npm/dm/react-use-resource)](https://www.npmjs.com/package/react-use-resource)\n[![Average time to resolve an issue](https://isitmaintained.com/badge/resolution/feerzlay/react-use-resource.svg)](https://isitmaintained.com/project/feerzlay/react-use-resource)\n\n## Installation\n\n```bash\nnpm install react-use-resource\n```\n\n```bash\nyarn add react-use-resource\n```\n\n## Usage\n\nWrap an application into a `ResourcesBoundary` component. All request results will be cached in there.\n\n```tsx\nimport { Suspense } from 'react';\nimport { ResourcesBoundary } from 'react-use-resource';\n\nexport function Application() {\n  return (\n    \u003cResourcesBoundary\u003e\n      \u003cSuspense fallback=\"Loading...\"\u003e\n        \u003cUser /\u003e\n      \u003c/Suspense\u003e\n    \u003c/ResourcesBoundary\u003e\n  );\n}\n```\n\nDeclare a promise returning function in any convenient way.\n\n```ts\ninterface IUser {\n  username: string;\n}\n\nexport function getUser(id: number) {\n  return fetch(`.../users/${id}`).then(response =\u003e response.json\u003cIUser\u003e());\n}\n```\n\nThe `useResource` hook takes a resource id, a promise returning function and a dependency list (which works simmilar to `useEffect` hook) and returns a resource for a provided function. Returned resource has `read` and `refresh` methods.\n\n```tsx\nimport { useResource } from 'react-use-resource';\n\nexport function User() {\n  const resource = useResource('USER', getUser, [42]);\n\n  return (\n    \u003ch1\u003e{ resource.read().username }\u003ch1\u003e\n  );\n};\n```\n\nResource id should be unique for a rendered component tree. Variables from a dependency list are passed down to a function as an arguments. Cached resource is invalidated upon a component unmount.\n\n### Request cancellation\n\nIdeally, upon a dependency list change we want to cancel a previous outgoing request. In order to achieve this our function should return not a simple promise but a tuple of a promise and a cancellation function.\n\n```ts\nexport function getUser(id: number): [Promise\u003cIUser\u003e, () =\u003e void] {\n  const controller = new AbortController();\n  const signal = controller.signal;\n\n  const promise = fetch(`.../users/${id}`, { signal }).then(response =\u003e response.json\u003cIUser\u003e());\n\n  return [promise, controller.abort];\n}\n```\n### Lazy resources\n\nInstead of `useResource` we can use `useLazyResource` hook. This way the resource will be requested only upon the `read` or `refresh` call.\n\n### SSR\n\nWe can pass a `cache` property to `ResourcesBoundary`. All data will be written to and read from this record.\n\n```tsx\nconst cache: Record\u003cstring, any\u003e = {};\n\n\u003cResourcesBoundary cache={cache}\u003e\n  \u003cApplication /\u003e\n\u003c/ResourcesBoundary\u003e\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffeerzlay%2Freact-use-resource","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffeerzlay%2Freact-use-resource","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffeerzlay%2Freact-use-resource/lists"}