{"id":13450641,"url":"https://github.com/awmleer/use-async-memo","last_synced_at":"2025-04-06T03:12:30.171Z","repository":{"id":34970504,"uuid":"193450168","full_name":"awmleer/use-async-memo","owner":"awmleer","description":"React hook for generating async memoized data.","archived":false,"fork":false,"pushed_at":"2023-12-01T06:35:54.000Z","size":359,"stargazers_count":140,"open_issues_count":3,"forks_count":9,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-23T23:46:34.335Z","etag":null,"topics":["async","hook","react","use-memo"],"latest_commit_sha":null,"homepage":null,"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/awmleer.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":"2019-06-24T06:49:34.000Z","updated_at":"2024-06-18T15:31:37.769Z","dependencies_parsed_at":"2024-06-18T15:31:32.662Z","dependency_job_id":"7ce8aed6-c8cc-4936-9b19-d0c17f94de07","html_url":"https://github.com/awmleer/use-async-memo","commit_stats":{"total_commits":36,"total_committers":5,"mean_commits":7.2,"dds":0.5833333333333333,"last_synced_commit":"6ec46fda170e354dae1056546505033d007c1617"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/awmleer%2Fuse-async-memo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/awmleer%2Fuse-async-memo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/awmleer%2Fuse-async-memo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/awmleer%2Fuse-async-memo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/awmleer","download_url":"https://codeload.github.com/awmleer/use-async-memo/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247427012,"owners_count":20937214,"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","hook","react","use-memo"],"created_at":"2024-07-31T07:00:36.909Z","updated_at":"2025-04-06T03:12:30.152Z","avatar_url":"https://github.com/awmleer.png","language":"TypeScript","funding_links":[],"categories":["Packages"],"sub_categories":[],"readme":"# useAsyncMemo\n\nReact hook for generating async memoized data.\n\n## API\n\n```typescript\nfunction useAsyncMemo\u003cT\u003e(factory: () =\u003e Promise\u003cT\u003e, deps: DependencyList, initial?: T): T\n```\n\nIf `factory` returns `undefined` or `null`, `useAsyncMemo` will leave the memoized value **unchanged**.\n\n## Demo\n\n### Import\n\n```js\nimport {useAsyncMemo} from \"use-async-memo\"\n```\n\n### Fetch API:\n\n```js\nconst data = useAsyncMemo(doAPIRequest, [])\n```\n\nor\n\n```js\nconst data = useAsyncMemo(() =\u003e doAPIRequest(), [])\n```\n\nor\n\n```js\nconst data = useAsyncMemo(() =\u003e {\n  return doAPIRequest()\n}, [])\n```\n\nor\n\n```js\nconst data = useAsyncMemo(async () =\u003e {\n  return await doAPIRequest()\n}, [])\n```\n\n### Search on inputting:\n\n```js\nconst [input, setInput] = useState()\nconst users = useAsyncMemo(async () =\u003e {\n  if (input === '') return []\n  return await apiService.searchUsers(input)\n}, [input], [])\n```\n\n### Get loading status:\n\n```js\nconst [loading, setLoading] = useState(true)\nconst data = useAsyncMemo(async () =\u003e {\n  setLoading(true)\n  const response = await doAPIRequest()\n  setLoading(false)\n  return response\n}, [])\n```\n\n### With ability of manual clearing:\n\n```js\nconst [input, setInput] = useState()\n\nconst [clearFlag, setClearFlag] = useState({\n  val: false\n})\nfunction clearItems() {\n  setClearFlag({\n    val: true\n  })\n}\n\nconst users = useAsyncMemo(async () =\u003e {\n  if (clearFlag.val) {\n    clearFlag.val = false\n    return null\n  }\n  if (input === '') return []\n  return await apiService.searchUsers(input)\n}, [input, clearFlag], [])\n```\n\n### With debounced value:\n \n\u003e see [use-debounce](https://github.com/xnimorz/use-debounce)\n\n```js\nconst [input, setInput] = useState()\nconst [debouncedInput] = useDebounce(input, 300)\nconst users = useAsyncMemo(async () =\u003e {\n  if (debouncedInput === '') return []\n  return await apiService.searchUsers(debouncedInput)\n}, [debouncedInput], [])\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fawmleer%2Fuse-async-memo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fawmleer%2Fuse-async-memo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fawmleer%2Fuse-async-memo/lists"}