{"id":20935127,"url":"https://github.com/ahungrynoob/use-root-reducer","last_synced_at":"2025-10-30T08:55:25.430Z","repository":{"id":57388334,"uuid":"220627890","full_name":"ahungrynoob/use-root-reducer","owner":"ahungrynoob","description":"A helper to create and maintain a global state without redux. (based on react hook)","archived":false,"fork":false,"pushed_at":"2019-11-12T12:03:46.000Z","size":14,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-21T03:09:48.607Z","etag":null,"topics":["hook","react","usecontext","usestate"],"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/ahungrynoob.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}},"created_at":"2019-11-09T10:46:01.000Z","updated_at":"2020-09-16T09:15:00.000Z","dependencies_parsed_at":"2022-09-02T06:02:04.375Z","dependency_job_id":null,"html_url":"https://github.com/ahungrynoob/use-root-reducer","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahungrynoob%2Fuse-root-reducer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahungrynoob%2Fuse-root-reducer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahungrynoob%2Fuse-root-reducer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahungrynoob%2Fuse-root-reducer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ahungrynoob","download_url":"https://codeload.github.com/ahungrynoob/use-root-reducer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254021227,"owners_count":22000887,"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":["hook","react","usecontext","usestate"],"created_at":"2024-11-18T22:13:07.332Z","updated_at":"2025-10-30T08:55:20.384Z","avatar_url":"https://github.com/ahungrynoob.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# use-root-reducer\n\n[![NPM version][npm-image]][npm-url]\n[![build status][travis-image]][travis-url]\n[![Test coverage][codecov-image]][codecov-url]\n[![npm download][download-image]][download-url]\n\n[npm-image]: https://img.shields.io/npm/v/use-root-reducer.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/use-root-reducer\n[travis-image]: https://img.shields.io/travis/ahungrynoob/use-root-reducer.svg?style=flat-square\n[travis-url]: https://travis-ci.org/ahungrynoob/use-root-reducer\n[codecov-image]: https://codecov.io/gh/ahungrynoob/use-root-reducer/branch/master/graph/badge.svg\n[codecov-url]: https://codecov.io/gh/ahungrynoob/use-root-reducer\n[download-image]: https://img.shields.io/npm/dm/use-root-reducer.svg?style=flat-square\n[download-url]: https://npmjs.org/package/use-root-reducer\n\nA helper to create and maintain a root state without redux. (based on react hooks)\n\u003cbr/\u003e\n**The implementation is quit simple:**\n\n1. Childcomponents access root state and root dispatch function throught [React Context API](https://reactjs.org/docs/context.html) and [useContext hook](https://reactjs.org/docs/hooks-reference.html#usecontext)\n2. When call the root dispatch, it will traverse your dispatches and pass action argv which are outputed by [useReducer hook](https://reactjs.org/docs/hooks-reference.html#usereducer).\n3. **The root dispatch function outputed by `use-root-reducer` is immutable in per functional component.**\n\n---\n\n## Install\n\n```bash\n$ npm i use-root-reducer --save\n```\n\n## Usage\n\n**First of all, create a root state and a root dispatch provider as `RootReducerProvider` in a independent file which can improve performance.[Avoiding unnecessary renders with React context](https://frontarm.com/james-k-nelson/react-context-performance/)**\n\n```jsx\n// context.jsx\nimport React, { Dispatch, useReducer } from \"react\";\nimport useRootReducer from \"use-root-reducer\";\nimport { RootAction } from \"../redux/action\";\nimport { fooReducer,barReducer } from \"../redux/reducer\";\n\nexport const StateContext = React.createContext({});\n\nexport const DispatchContext = React.createContext(null);\n\nexport const RootReducerProvider = ({ children, foo, bar }) =\u003e {\n  const [state, dispatch] = useRootReducer({\n    foo: useReducer(fooReducer, \"foo\")\n    bar: useReducer(barReducer, \"bar\")\n  });\n  return (\n    \u003cDispatchContext.Provider value={dispatch}\u003e\n      \u003cStateContext.Provider value={state}\u003e{children}\u003c/StateContext.Provider\u003e\n    \u003c/DispatchContext.Provider\u003e\n  );\n};\n```\n\n**Second, import the root `RootReducerProvider` to your root component such as app.jsx**\n\n```jsx\n// app.jsx\nimport React from \"react\";\nimport { BrowserRouter, StaticRouter, Switch, Route } from \"react-router-dom\";\nimport NotFound from \"./notfound\";\nimport Content from \"./content\";\nimport Home from \"./home\";\nimport { RootReducerProvider } from \"./context\";\n\nconst Router = __CLIENT__ ? BrowserRouter : StaticRouter;\n\nconst App = props =\u003e {\n  const { location: staticLocation, context, bgIndex, foo, bar } = props;\n  return (\n    \u003cRouter location={staticLocation} context={context}\u003e\n      \u003cRootReducerProvider foo={foo} bar={bar}\u003e\n        \u003cdiv\u003echildren component\u003c/div\u003e\n      \u003c/RootReducerProvider\u003e\n    \u003c/Router\u003e\n  );\n};\n\nexport default App;\n```\n\n**In the end, in your child components you can access your root state and root dispatch with `useContext`:**\n\n```jsx\n//\nimport React from \"react\";\n\nimport { StateContext, DispatchContext } from \"./context.js\";\n\nexport default () =\u003e {\n  const state = React.useContext(StateContext);\n  const dispatch = React.useContext(DispatchContext);\n\n  const { foo, bar } = state;\n\n  return (\n    \u003cbutton\n      onClick={() =\u003e {\n        // dispatch your action and will be received in all your reducers\n        dispatch({ type: \"update\", payload: \"foo updated\", meta: \"foo\" });\n      }}\n    \u003e{`${foo} and ${bar}`}\u003c/button\u003e\n  );\n};\n```\n\n## License\n\n[MIT](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fahungrynoob%2Fuse-root-reducer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fahungrynoob%2Fuse-root-reducer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fahungrynoob%2Fuse-root-reducer/lists"}