{"id":14987604,"url":"https://github.com/zoontek/react-atomic-state","last_synced_at":"2025-04-06T17:13:02.246Z","repository":{"id":41575272,"uuid":"283274655","full_name":"zoontek/react-atomic-state","owner":"zoontek","description":"Dead simple React global state management based on use-sync-external-store.","archived":false,"fork":false,"pushed_at":"2025-01-28T13:46:57.000Z","size":1661,"stargazers_count":48,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-30T15:11:28.015Z","etag":null,"topics":[],"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/zoontek.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","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},"funding":{"github":["zoontek"]}},"created_at":"2020-07-28T16:56:22.000Z","updated_at":"2025-01-28T13:46:59.000Z","dependencies_parsed_at":"2023-07-25T07:28:54.406Z","dependency_job_id":"bbc0a247-a081-4230-8c69-8d301fa3645e","html_url":"https://github.com/zoontek/react-atomic-state","commit_stats":{"total_commits":90,"total_committers":3,"mean_commits":30.0,"dds":0.2777777777777778,"last_synced_commit":"d2d3ec88576f7a1f0dd19edc2fca713099af3446"},"previous_names":["zoontek/react-global-state"],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zoontek%2Freact-atomic-state","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zoontek%2Freact-atomic-state/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zoontek%2Freact-atomic-state/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zoontek%2Freact-atomic-state/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zoontek","download_url":"https://codeload.github.com/zoontek/react-atomic-state/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247517916,"owners_count":20951719,"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-24T14:14:59.892Z","updated_at":"2025-04-06T17:13:02.215Z","avatar_url":"https://github.com/zoontek.png","language":"TypeScript","funding_links":["https://github.com/sponsors/zoontek"],"categories":[],"sub_categories":[],"readme":"# ⚛️ react-atomic-state\n\nDead simple React global state management based on [`use-sync-external-store`](https://github.com/facebook/react/tree/master/packages/use-sync-external-store).\n\n[![mit licence](https://img.shields.io/dub/l/vibe-d.svg?style=for-the-badge)](https://github.com/zoontek/react-atomic-state/blob/main/LICENSE)\n[![npm version](https://img.shields.io/npm/v/react-atomic-state?style=for-the-badge)](https://www.npmjs.org/package/react-atomic-state)\n[![bundlephobia](https://img.shields.io/bundlephobia/minzip/react-atomic-state?label=size\u0026style=for-the-badge)](https://bundlephobia.com/result?p=react-atomic-state)\n\n## Installation\n\n```bash\n$ npm install --save react-atomic-state\n# --- or ---\n$ yarn add react-atomic-state\n```\n\n## ❓Motivation\n\nI'm a **huge** fan of the _\"state and props\"_ couple, but sometimes I need to share a simple value to my entire application.\u003cbr /\u003e\nAs I'm not a fan of the `Context` API and found existing global state management libraries overkill to me most of the times, I decided to publish this small library to cover this specific need 🙌.\n\n## Usage\n\n```tsx\n// states/count.ts\nimport { atom, useAtom, useAtomWithSelector } from \"react-atomic-state\";\n\nconst count = atom(0);\n\nexport const decrement = () =\u003e count.set((prevCount) =\u003e prevCount - 1);\nexport const increment = () =\u003e count.set((prevCount) =\u003e prevCount + 1);\n\nconst unsubscribe = count.subscribe((value) =\u003e {\n  console.log(value); // log every update\n});\n\n// create a custom hook\nexport const useCount = () =\u003e useAtom(count);\n\n// create a custom hook with selector\n// (not to create a complex object store - it's often better to create multiple atoms, but to derive data)\nexport const useStringCount = () =\u003e\n  useAtomWithSelector(count, (count) =\u003e count.toString());\n```\n\n```tsx\nimport { decrement, increment, useCount } from \"./states/count.ts\";\n\nconst Counter = () =\u003e {\n  const count = useCount();\n\n  return (\n    \u003cdiv\u003e\n      \u003cspan\u003ecount: {count}\u003c/span\u003e\n\n      \u003cbutton onClick={decrement}\u003e-1\u003c/button\u003e\n      \u003cbutton onClick={increment}\u003e+1\u003c/button\u003e\n    \u003c/div\u003e\n  );\n};\n```\n\n## API\n\n### atom()\n\n```ts\ntype atom = \u003cValue\u003e(initialValue: Value) =\u003e {\n  get: () =\u003e Value;\n  set: (value: Value | ((prevValue: Value) =\u003e Value)) =\u003e void;\n  subscribe: (callback: (value: Value) =\u003e void) =\u003e () =\u003e void;\n  reset: () =\u003e void;\n};\n```\n\n### useAtom()\n\n```ts\ntype useAtom = \u003cValue\u003e(\n  atom: Atom\u003cValue\u003e,\n  isEqual?: (prevValue: Value, nextValue: Value) =\u003e boolean,\n) =\u003e Value;\n```\n\n### useAtomWithSelector()\n\n```ts\ntype useAtomWithSelector = \u003cValue, Selection\u003e(\n  atom: Atom\u003cValue\u003e,\n  selector: (value: Value) =\u003e Selection,\n  isEqual?: (prevSelection: Selection, nextSelection: Selection) =\u003e boolean,\n) =\u003e Value;\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzoontek%2Freact-atomic-state","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzoontek%2Freact-atomic-state","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzoontek%2Freact-atomic-state/lists"}