{"id":18369819,"url":"https://github.com/cawfree/react-use-mutator","last_synced_at":"2025-04-06T18:32:33.089Z","repository":{"id":47370610,"uuid":"227347489","full_name":"cawfree/react-use-mutator","owner":"cawfree","description":"A React hook for inspecting and mutating shared state without subscribing to render updates.","archived":false,"fork":false,"pushed_at":"2023-01-05T02:52:20.000Z","size":438,"stargazers_count":5,"open_issues_count":8,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-23T07:32:18.736Z","etag":null,"topics":["hooks","mutate","react","react-native","shared","state","sync","use"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/cawfree.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":"2019-12-11T11:15:32.000Z","updated_at":"2022-05-10T18:24:30.000Z","dependencies_parsed_at":"2023-02-03T07:16:51.640Z","dependency_job_id":null,"html_url":"https://github.com/cawfree/react-use-mutator","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cawfree%2Freact-use-mutator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cawfree%2Freact-use-mutator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cawfree%2Freact-use-mutator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cawfree%2Freact-use-mutator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cawfree","download_url":"https://codeload.github.com/cawfree/react-use-mutator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247531319,"owners_count":20953934,"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":["hooks","mutate","react","react-native","shared","state","sync","use"],"created_at":"2024-11-05T23:32:21.038Z","updated_at":"2025-04-06T18:32:31.269Z","avatar_url":"https://github.com/cawfree.png","language":"JavaScript","funding_links":["https://www.buymeacoffee.com/cawfree"],"categories":[],"sub_categories":[],"readme":"# react-use-mutator\nA React hook for inspecting and mutating shared state without subscribing to render updates.\n\n## 🚀 Getting Started\n\nUsing [`npm`]():\n\n```bash\nnpm install --save react-use-mutator\n```\n\nUsing [`yarn`]():\n\n```bash\nyarn add react-use-mutator\n```\n\n## 🤔 What is this for?\n\nSome applications depend on `useState` to manage a value which can be both consumed by and written to by many children, but because of the way [React]() updates your components, their changes have the chance to overwrite or not reflect upon previous changes since the last `render`.\n\nAdditionally, sometimes it is useful to interrogate the value of the state held by a hook, without necessarily wanting to _subscribe_ to those changes.\n\n`react-use-mutator` enables us to both predictably update shared state, and inspect the current value of that state without subscribing to it.\n\n## ✍️ Example\n\nIn this example, we render `5000` children who all have shared access to the global state, who on mount, all attempt to register their unique identifier. Without using mutations, printing to the `console` in our `useLayoutEffect` hook only ever retain the contents of a single key, since all `children` effectively complete to register against the initial, empty state.\n\nBy contrast, `useMutator` allows us to register all `5000` children safely, _without_ an insane amount of render updates. This takes just a single render operation!\n\n```javascript\nimport React, { useContext, useEffect, useLayoutEffect } from 'react';\nimport uuidv4 from 'uuid/v4';\nimport { Map } from 'immutable';\nimport { useMutator } from 'react-use-mutator';\n\nconst StateContext = React.createContext();\nconst MutatorContext = React.createContext();\n\nconst Child = ({ ...extraProps }) =\u003e {\n  // XXX: Registers this child to the currently mounted\n  //      value of the StateContext in the DOM.\n  const currentState = useContext(StateContext)();\n  const mutateState = useContext(MutatorContext);\n  const [ myId ] = useState(\n    () =\u003e uuidv4(),\n  );\n  useEffect(\n    () =\u003e {\n      mutateState(\n        currentState =\u003e currentState\n          .set(myId, true),\n      );\n    },\n    [],\n  );\n  return null;\n};\n\nexport default () =\u003e {\n  const [ useMutations, mutate ] = useMutator(\n    () =\u003e Map({}),\n  );\n  useLayoutEffect(\n    // XXX: The current value of the state can be used any times by calling mutate().\n    //      mutate() normally expects a mutation function, but if this is not provided,\n    //      it terminates early with the value of the current state.\n    () =\u003e console.log(JSON.stringify(mutate()));\n  );\n  return (\n    \u003cMutatorContext.Provider\n      value={mutate}\n    \u003e\n      \u003cStateContext.Provider\n        value={useMutations}\n      \u003e\n        {[...Array(5000)]   \n          .map(\n            (_, i) =\u003e (\n              \u003cChild\n                key={i}\n              /\u003e\n            ),\n          )}\n      \u003c/StateContext.Provider\u003e\n    \u003c/MutatorContext.Provider\u003e\n  );\n};\n\n```\n\n## ✌️ License\n[MIT](https://opensource.org/licenses/MIT)\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://www.buymeacoffee.com/cawfree\"\u003e\n    \u003cimg src=\"https://cdn.buymeacoffee.com/buttons/default-orange.png\" alt=\"Buy @cawfree a coffee\" width=\"232\" height=\"50\" /\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcawfree%2Freact-use-mutator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcawfree%2Freact-use-mutator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcawfree%2Freact-use-mutator/lists"}