{"id":15275833,"url":"https://github.com/zustandjs/use-zustand","last_synced_at":"2025-04-08T02:36:34.997Z","repository":{"id":61521256,"uuid":"552173641","full_name":"zustandjs/use-zustand","owner":"zustandjs","description":"Another custom hook to use Zustand vanilla store","archived":false,"fork":false,"pushed_at":"2025-02-25T00:32:46.000Z","size":344,"stargazers_count":105,"open_issues_count":1,"forks_count":3,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-01T01:36:41.728Z","etag":null,"topics":[],"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/zustandjs.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-10-16T01:40:42.000Z","updated_at":"2025-03-30T00:49:53.000Z","dependencies_parsed_at":"2025-02-09T01:05:23.821Z","dependency_job_id":"299aac1c-5895-43cd-8db7-4e8b3b16c027","html_url":"https://github.com/zustandjs/use-zustand","commit_stats":null,"previous_names":["dai-shi/use-zustand"],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zustandjs%2Fuse-zustand","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zustandjs%2Fuse-zustand/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zustandjs%2Fuse-zustand/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zustandjs%2Fuse-zustand/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zustandjs","download_url":"https://codeload.github.com/zustandjs/use-zustand/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247765533,"owners_count":20992325,"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":[],"created_at":"2024-09-30T10:07:11.101Z","updated_at":"2025-04-08T02:36:34.976Z","avatar_url":"https://github.com/zustandjs.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# use-zustand\n\n[![CI](https://img.shields.io/github/actions/workflow/status/zustandjs/use-zustand/ci.yml?branch=main)](https://github.com/zustandjs/use-zustand/actions?query=workflow%3ACI)\n[![npm](https://img.shields.io/npm/v/use-zustand)](https://www.npmjs.com/package/use-zustand)\n[![size](https://img.shields.io/bundlephobia/minzip/use-zustand)](https://bundlephobia.com/result?p=use-zustand)\n[![discord](https://img.shields.io/discord/627656437971288081)](https://discord.gg/MrQdmzd)\n\nAnother custom hook to use [Zustand](https://github.com/pmndrs/zustand) vanilla store\n\n## Install\n\n```bash\nnpm install zustand use-zustand\n```\n\n## Usage\n\n```jsx\nimport { createStore } from 'zustand/vanilla';\nimport { useZustand } from 'use-zustand';\n\nconst countStore = createStore((set) =\u003e ({\n  count: 0,\n  inc: () =\u003e set((state) =\u003e ({ count: state.count + 1 })),\n}));\n\nconst Counter = () =\u003e {\n  const count = useZustand(countStore, (state) =\u003e state.count);\n  const inc = useZustand(countStore, (state) =\u003e state.inc);\n  return (\n    \u003cdiv\u003e\n      {count} \u003cbutton onClick={inc}\u003e+1\u003c/button\u003e\n    \u003c/div\u003e\n  );\n};\n```\n\n## But, why?\n\nZustand has `useStore` hook that can be used with vanilla store,\nwhich is identical to `useZustand` in terms of the usage.\n\n```jsx\nimport { createStore, useStore } from 'zustand';\n\n// `createStore` is the same with:\nimport { createStore } from 'zustand/vanilla';\n```\n\n`useStore` is implemented with `useSyncExternalStore` which is\na recommended way to use \"external stores\". It solves tearing issues.\n\nHowever, the \"Sync\" behavior doesn't work nicely with concurrent rendering.\nWe can't use `startTransition` as expected.\n\n`useZustand` doesn't use `useSyncExternalStore`,\nbut only `useReducer` and `useEffect`.\nIt suffers from tearing, but works better with concurrent rendering.\n\nAfter all, it's a trade-off.\n\n## Examples\n\nThe [examples](examples) folder contains working examples.\nYou can run one of them with\n\n```bash\nPORT=8080 pnpm run examples:01_counter\n```\n\nand open \u003chttp://localhost:8080\u003e in your web browser.\n\nYou can also try them directly:\n[01](https://stackblitz.com/github/zustandjs/use-zustand/tree/main/examples/01_counter)\n[02](https://stackblitz.com/github/zustandjs/use-zustand/tree/main/examples/02_suspense)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzustandjs%2Fuse-zustand","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzustandjs%2Fuse-zustand","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzustandjs%2Fuse-zustand/lists"}