{"id":17982404,"url":"https://github.com/nekocode/use-shared-state","last_synced_at":"2025-07-09T23:32:55.249Z","repository":{"id":41064558,"uuid":"234349127","full_name":"nekocode/use-shared-state","owner":"nekocode","description":"React hook for sharing state or notifying event between components. Just like the widget controller in Flutter.","archived":false,"fork":false,"pushed_at":"2023-11-29T04:38:50.000Z","size":1021,"stargazers_count":60,"open_issues_count":2,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-06-09T05:51:22.618Z","etag":null,"topics":["web"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/@nekocode/use-shared-state","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/nekocode.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}},"created_at":"2020-01-16T15:22:14.000Z","updated_at":"2025-04-08T01:53:58.000Z","dependencies_parsed_at":"2023-11-29T05:27:42.629Z","dependency_job_id":"15a27f95-bf7c-432d-b6c0-1ccc7272c203","html_url":"https://github.com/nekocode/use-shared-state","commit_stats":{"total_commits":74,"total_committers":1,"mean_commits":74.0,"dds":0.0,"last_synced_commit":"59a1a4a1e8cdda677b9970d82072759045215c7b"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/nekocode/use-shared-state","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nekocode%2Fuse-shared-state","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nekocode%2Fuse-shared-state/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nekocode%2Fuse-shared-state/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nekocode%2Fuse-shared-state/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nekocode","download_url":"https://codeload.github.com/nekocode/use-shared-state/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nekocode%2Fuse-shared-state/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264505263,"owners_count":23618911,"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":["web"],"created_at":"2024-10-29T18:14:09.851Z","updated_at":"2025-07-09T23:32:55.225Z","avatar_url":"https://github.com/nekocode.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![npm](https://img.shields.io/npm/v/@nekocode/use-shared-state)](https://www.npmjs.com/package/@nekocode/use-shared-state) ![npm bundle size](https://img.shields.io/bundlephobia/minzip/@nekocode/use-shared-state) [![ci](https://github.com/nekocode/use-shared-state/actions/workflows/ci.yml/badge.svg)](https://github.com/nekocode/use-shared-state/actions/workflows/ci.yml)\n\n:octopus: React hook for sharing state between components, inspired by the [InheritedWidget](https://api.flutter.dev/flutter/widgets/InheritedWidget-class.html) in Flutter.\n\n`yarn add @nekocode/use-shared-state`\n\n**_Why choose it?_**\n\n1. It's lightweight and includes just over 100 lines of source code, making it very suitable for use in component or library projects\n2. Update components within a minimum range. For example, if we share a shared-state with components b and e, only these components will be updated when the shared-state updates\n\n```\n  a\n+-+-+\n| | |\nb c d\n    |\n    e\n```\n\n_相关文章: [use-shared-state 源码阅读](https://github.com/acfasj/blog/issues/3)_\n\nLive demo:\n\n[![Edit useSharedState - example](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/mystifying-cray-x2gcp?fontsize=14\u0026hidenavigation=1\u0026theme=dark)\n\n## Usage\n\n### 1. Share state between components\n\nThe simplest usage:\n\n```tsx\nconst CounterContext = React.createContext(new SharedState(0));\n\nconst ComponentA = () =\u003e {\n  const sharedState = React.useContext(CounterContext);\n  const [state, setState] = useSharedState(sharedState);\n  return (\n    \u003cdiv\u003e\n      \u003cdiv\u003estate: {state}\u003c/div\u003e\n      \u003cbutton onClick={() =\u003e setState(state + 1)}\u003eincrease\u003c/button\u003e\n    \u003c/div\u003e\n  );\n};\n\nconst ComponentB = () =\u003e {\n  const sharedState = React.useContext(CounterContext);\n  const [state] = useSharedState(sharedState);\n  return \u003cdiv\u003estate: {state}\u003c/div\u003e;\n};\n\nconst App = () =\u003e {\n  return (\n    \u003cCounterContext.Provider value={new SharedState(0)}\u003e\n      \u003cComponentA /\u003e\n      \u003cComponentB /\u003e\n    \u003c/CounterContext.Provider\u003e\n  );\n};\n```\n\nAdvanced usage:\n\n```tsx\nconst sharedSate = useMemo(() =\u003e new SharedState(0), []);\n\n// Only retrieve the current state value, the component will never be updated\nconst [state] = useSharedState(sharedState, false);\n\n// Update the component only when the current state value is greater than 1\nconst [state] = useSharedState(sharedState, (current, prev) =\u003e current \u003e 1);\n\n// Change the value of the shared state without updating hooked components\nconst [, setState] = useSharedState(sharedState);\nsetState(1, false);\n\n// When the first parameter (shared state) is null, use 1 as the initial state value\nconst [state] = useSharedState(null, true, 1);\n```\n\n### 2. Notify event between components\n\nUse `ChangeNotifier`, `ValueNotifier`, and `useListen` to notify another component to invoke some callbacks. Please note that, unlike `useSharedState`, `useListen` will not trigger the re-rendering of the hooked component:\n\n```tsx\nconst refetchNotifier = new ChangeNotifier();\nconst eventNotifier = new ValueNotifier\u003cstring | null\u003e(null);\n\nconst ComponentA = () =\u003e {\n  // ...\n  useListen(refetchNotifier, () =\u003e refetch());\n  useListen(eventNotifier, (value) =\u003e {\n    if (value === 'setState') {\n      setState();\n    }\n  });\n  // ...\n};\n\n// In component B, call notifyListeners/setValue to notify A\nconst ComponentB = () =\u003e {\n  // ...\n  refetchNotifier.notifyListeners();\n  eventNotifier.setValue('setState');\n  // ...\n};\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnekocode%2Fuse-shared-state","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnekocode%2Fuse-shared-state","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnekocode%2Fuse-shared-state/lists"}