{"id":22827292,"url":"https://github.com/narinluangrath/react-create-array-context","last_synced_at":"2026-05-20T05:32:40.865Z","repository":{"id":65482941,"uuid":"408535279","full_name":"narinluangrath/react-create-array-context","owner":"narinluangrath","description":"Efficiently use React Context with arrays","archived":false,"fork":false,"pushed_at":"2021-09-21T20:44:28.000Z","size":587,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-09T14:22:23.863Z","etag":null,"topics":["bitwise-operators","calculatechangedbits","context","react","react-context-api"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/narinluangrath.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-09-20T17:19:46.000Z","updated_at":"2021-09-21T21:14:23.000Z","dependencies_parsed_at":"2023-01-25T16:46:44.992Z","dependency_job_id":null,"html_url":"https://github.com/narinluangrath/react-create-array-context","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/narinluangrath/react-create-array-context","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/narinluangrath%2Freact-create-array-context","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/narinluangrath%2Freact-create-array-context/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/narinluangrath%2Freact-create-array-context/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/narinluangrath%2Freact-create-array-context/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/narinluangrath","download_url":"https://codeload.github.com/narinluangrath/react-create-array-context/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/narinluangrath%2Freact-create-array-context/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271569796,"owners_count":24782482,"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","status":"online","status_checked_at":"2025-08-21T02:00:08.990Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["bitwise-operators","calculatechangedbits","context","react","react-context-api"],"created_at":"2024-12-12T18:09:00.051Z","updated_at":"2026-05-20T05:32:35.843Z","avatar_url":"https://github.com/narinluangrath.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# React Create Array Context\n\nEfficiently use React Context with arrays\n\n## Motivation\n\nThe [React Context API](https://reactjs.org/docs/context.html) has a [hidden second argument](https://github.com/facebook/react/blob/c390ab3643612dc08ca4bebadc5b0377e9e7eb79/packages/react/src/ReactContext.js#L14) called `calculateChangedBits`, which you can use as a [performance optimization](https://dev.to/alexkhismatulin/react-context-a-hidden-power-3h8j) to reduce unecessary renders to context consumers. However, using this API involes knowledge of [bitwise operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators) which makes it tricky to use. This library implements the details of `calculateChangedBits` (and `unstable_observedBits`) for you, **assuming your context state is an array**.\n\nTo be honest, I'm not sure this library is useful. If you're experiencing performance issues with React Context, consider using some of the solutions suggested [here](https://github.com/facebook/react/issues/15156#issuecomment-474590693) before reaching for this library.\n## Getting Started\n\n### Install\n\n```bash\nnpm install --save react-create-array-context\n\n# Or yarn\n# yarn add react-create-array-context\n```\n\n### Basic Usage\n\n```jsx\nconst [ArrayContextProvider, useArrayContext] = createArrayContext();\n\n// This component will rerender _only_ when the 0th element\n// of the context array changes (initially 'substate0')\nfunction ArrayContextConsumer0() {\n  const [contextState, setContextState] = useArrayContext([0]);\n\n  const handleChange = (e) =\u003e {\n    const { value } = e.taraget;\n    // 🚨 You must use the callback version when setting\n    // 🚨 state because `contextState` may be stale (since it\n    // 🚨 doesn't update when 1st or 2nd element change)\n    setContextState(arr =\u003e {\n      const copy = [ ...arr ];\n      copy[0] = value;\n      return copy;\n    })\n  }\n\n  return \u003cinput onChange={handleChange} value={contextState[0]} /\u003e\n}\n\n// This component will rerender _only_ when the 0th or 1st element\n// of the context array changes\nfunction ArrayContextConsumer12() {\n  const [contextState, setContextState] = useArrayContext([1, 2]);\n\n  // Do something with contextState[0] and contextState[1]\n}\n\n// This component will rerender when any element of the context array changes\nfunction ArrayContextConsumer() {\n  const [contextState, setContextState] = useArrayContext();\n\n  // Do something with contextState\n}\n\nfunction App() {\n  \u003cArrayContextProvider initialState={['substate0', 'substate1', 'substate2']}\u003e\n    \u003cArrayContextConsumer0 /\u003e\n    \u003cArrayContextConsumer12 /\u003e\n    \u003cArrayContextConsumer /\u003e\n  \u003c/ArrayContextProvider\u003e\n}\n```\n\n### Caveats\n\n1. You must use the callback version of setting state (see 🚨 emoji above).\n2. Because of `calculateChangedBits` only has 32 bits to work with, arrays longer than 32 elements may trigger unnecessary rerenders (but it will still be more efficient than not using this library).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnarinluangrath%2Freact-create-array-context","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnarinluangrath%2Freact-create-array-context","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnarinluangrath%2Freact-create-array-context/lists"}