{"id":13450684,"url":"https://github.com/dai-shi/use-context-selector","last_synced_at":"2025-05-15T14:06:14.787Z","repository":{"id":35000858,"uuid":"195327838","full_name":"dai-shi/use-context-selector","owner":"dai-shi","description":"React useContextSelector hook in userland","archived":false,"fork":false,"pushed_at":"2025-04-04T23:07:20.000Z","size":2596,"stargazers_count":2805,"open_issues_count":7,"forks_count":61,"subscribers_count":12,"default_branch":"main","last_synced_at":"2025-04-07T20:07:52.238Z","etag":null,"topics":["custom-hook","react","react-context","react-hooks"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/use-context-selector","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/dai-shi.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":"2019-07-05T02:46:26.000Z","updated_at":"2025-04-07T07:55:15.000Z","dependencies_parsed_at":"2023-12-01T02:32:38.602Z","dependency_job_id":"28692d5a-07cf-49fb-82b5-76fb7cc4b3f4","html_url":"https://github.com/dai-shi/use-context-selector","commit_stats":{"total_commits":201,"total_committers":9,"mean_commits":"22.333333333333332","dds":"0.19402985074626866","last_synced_commit":"76cf255787e1424ed63b8cda4c621ef793b4c38b"},"previous_names":[],"tags_count":44,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dai-shi%2Fuse-context-selector","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dai-shi%2Fuse-context-selector/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dai-shi%2Fuse-context-selector/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dai-shi%2Fuse-context-selector/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dai-shi","download_url":"https://codeload.github.com/dai-shi/use-context-selector/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254355335,"owners_count":22057354,"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":["custom-hook","react","react-context","react-hooks"],"created_at":"2024-07-31T07:00:37.294Z","updated_at":"2025-05-15T14:06:09.764Z","avatar_url":"https://github.com/dai-shi.png","language":"TypeScript","funding_links":[],"categories":["Packages","TypeScript","JavaScript"],"sub_categories":[],"readme":"# use-context-selector\n\n[![CI](https://img.shields.io/github/actions/workflow/status/dai-shi/use-context-selector/ci.yml?branch=main)](https://github.com/dai-shi/use-context-selector/actions?query=workflow%3ACI)\n[![npm](https://img.shields.io/npm/v/use-context-selector)](https://www.npmjs.com/package/use-context-selector)\n[![size](https://img.shields.io/bundlephobia/minzip/use-context-selector)](https://bundlephobia.com/result?p=use-context-selector)\n[![discord](https://img.shields.io/discord/627656437971288081)](https://discord.gg/MrQdmzd)\n\nReact useContextSelector hook in userland\n\n## Introduction\n\nReact Context and useContext is often used to avoid prop drilling,\nhowever it's known that there's a performance issue.\nWhen a context value is changed, all components that useContext\nwill re-render.\n\nTo solve this issue,\n[useContextSelector](https://github.com/reactjs/rfcs/pull/119)\nis proposed and later proposed\n[Speculative Mode](https://github.com/reactjs/rfcs/pull/150)\nwith context selector support.\nThis library provides the API in userland.\n\nPrior to v1.3, it uses `changedBits=0` feature to stop propagation,\nv1.3 no longer depends on this undocumented feature.\n\n## Install\n\nThis package requires some peer dependencies, which you need to install by yourself.\n\n```bash\nnpm install use-context-selector react scheduler\n```\n\nNotes for library authors:\n\nPlease do not forget to keep `\"peerDependencies\"` and\nnote instructions to let users to install peer dependencies.\n\n## Technical memo\n\nTo make it work like original React context, it uses\n[useReducer cheat mode](https://overreacted.io/a-complete-guide-to-useeffect/#why-usereducer-is-the-cheat-mode-of-hooks) intentionally.\n\nIt also requires `useContextUpdate` to behave better in concurrent rendering.\nIts usage is optional and only required if the default behavior is unexpected.\n\nIf you need a simpler solution, you can use `useSyncExternalStore` without any libraries. See [an example](https://github.com/dai-shi/use-context-selector/issues/109#issuecomment-1785147682).\n\n## Usage\n\n```javascript\nimport { useState } from 'react';\nimport { createRoot } from 'react-dom/client';\n\nimport { createContext, useContextSelector } from 'use-context-selector';\n\nconst context = createContext(null);\n\nconst Counter1 = () =\u003e {\n  const count1 = useContextSelector(context, (v) =\u003e v[0].count1);\n  const setState = useContextSelector(context, (v) =\u003e v[1]);\n  const increment = () =\u003e\n    setState((s) =\u003e ({\n      ...s,\n      count1: s.count1 + 1,\n    }));\n  return (\n    \u003cdiv\u003e\n      \u003cspan\u003eCount1: {count1}\u003c/span\u003e\n      \u003cbutton type=\"button\" onClick={increment}\u003e\n        +1\n      \u003c/button\u003e\n      {Math.random()}\n    \u003c/div\u003e\n  );\n};\n\nconst Counter2 = () =\u003e {\n  const count2 = useContextSelector(context, (v) =\u003e v[0].count2);\n  const setState = useContextSelector(context, (v) =\u003e v[1]);\n  const increment = () =\u003e\n    setState((s) =\u003e ({\n      ...s,\n      count2: s.count2 + 1,\n    }));\n  return (\n    \u003cdiv\u003e\n      \u003cspan\u003eCount2: {count2}\u003c/span\u003e\n      \u003cbutton type=\"button\" onClick={increment}\u003e\n        +1\n      \u003c/button\u003e\n      {Math.random()}\n    \u003c/div\u003e\n  );\n};\n\nconst StateProvider = ({ children }) =\u003e (\n  \u003ccontext.Provider value={useState({ count1: 0, count2: 0 })}\u003e\n    {children}\n  \u003c/context.Provider\u003e\n);\n\nconst App = () =\u003e (\n  \u003cStateProvider\u003e\n    \u003cCounter1 /\u003e\n    \u003cCounter2 /\u003e\n  \u003c/StateProvider\u003e\n);\n\ncreateRoot(document.getElementById('app')).render(\u003cApp /\u003e);\n```\n\n## API\n\n\u003c!-- Generated by documentation.js. Update this documentation by updating the source code. --\u003e\n\n### createContext\n\nThis creates a special context for `useContextSelector`.\n\n#### Parameters\n\n*   `defaultValue` **Value**\u0026#x20;\n\n#### Examples\n\n```javascript\nimport { createContext } from 'use-context-selector';\n\nconst PersonContext = createContext({ firstName: '', familyName: '' });\n```\n\n### useContextSelector\n\nThis hook returns context selected value by selector.\n\nIt will only accept context created by `createContext`.\nIt will trigger re-render if only the selected value is referentially changed.\n\nThe selector should return referentially equal result for same input for better performance.\n\n#### Parameters\n\n*   `context` **Context\\\u003cValue\u003e**\u0026#x20;\n*   `selector` **function (value: Value): Selected**\u0026#x20;\n\n#### Examples\n\n```javascript\nimport { useContextSelector } from 'use-context-selector';\n\nconst firstName = useContextSelector(PersonContext, (state) =\u003e state.firstName);\n```\n\n### useContext\n\nThis hook returns the entire context value.\nUse this instead of React.useContext for consistent behavior.\n\n#### Parameters\n\n*   `context` **Context\\\u003cValue\u003e**\u0026#x20;\n\n#### Examples\n\n```javascript\nimport { useContext } from 'use-context-selector';\n\nconst person = useContext(PersonContext);\n```\n\n### useContextUpdate\n\nThis hook returns an update function to wrap an updating function\n\nUse this for a function that will change a value in\nconcurrent rendering in React 18.\nOtherwise, there's no need to use this hook.\n\n#### Parameters\n\n*   `context` **Context\\\u003cValue\u003e**\u0026#x20;\n\n#### Examples\n\n```javascript\nimport { useContextUpdate } from 'use-context-selector';\n\nconst update = useContextUpdate();\n\n// Wrap set state function\nupdate(() =\u003e setState(...));\n\n// Experimental suspense mode\nupdate(() =\u003e setState(...), { suspense: true });\n```\n\n### BridgeProvider\n\nThis is a Provider component for bridging multiple react roots\n\n#### Parameters\n\n*   `$0` **{context: Context\\\u003cany\u003e, value: any, children: ReactNode}**\u0026#x20;\n\n    *   `$0.context` \u0026#x20;\n    *   `$0.value` \u0026#x20;\n    *   `$0.children` \u0026#x20;\n\n#### Examples\n\n```javascript\nconst valueToBridge = useBridgeValue(PersonContext);\nreturn (\n  \u003cRenderer\u003e\n    \u003cBridgeProvider context={PersonContext} value={valueToBridge}\u003e\n      {children}\n    \u003c/BridgeProvider\u003e\n  \u003c/Renderer\u003e\n);\n```\n\n### useBridgeValue\n\nThis hook return a value for BridgeProvider\n\n#### Parameters\n\n*   `context` **Context\\\u003cany\u003e**\u0026#x20;\n\n## Limitations\n\n*   In order to stop propagation, `children` of a context provider has to be either created outside of the provider or memoized with `React.memo`.\n*   Provider trigger re-renders only if the context value is referentially changed.\n*   Neither context consumers or class components are supported.\n*   The [stale props](https://react-redux.js.org/api/hooks#stale-props-and-zombie-children) issue exists in React 17 and below. (Can be resolved with `unstable_batchedUpdates`)\n*   Tearing is only avoided if all consumers get data using `useContextSelector`. If you use both props and `use-context-selector` to pass the same data, they may provide inconsistence data for a brief moment. (`02_tearing_spec` fails)\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/dai-shi/use-context-selector/tree/main/examples/01_counter)\n[02](https://stackblitz.com/github/dai-shi/use-context-selector/tree/main/examples/02_person)\n[03](https://stackblitz.com/github/dai-shi/use-context-selector/tree/main/examples/03_suspense)\n\n## Projects that use use-context-selector\n\n*   [react-tracked](https://github.com/dai-shi/react-tracked)\n*   [use-atom](https://github.com/dai-shi/use-atom)\n*   [react-hooks-fetch](https://github.com/dai-shi/react-hooks-fetch)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdai-shi%2Fuse-context-selector","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdai-shi%2Fuse-context-selector","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdai-shi%2Fuse-context-selector/lists"}