{"id":19636630,"url":"https://github.com/mizchi/use-react-redux-context","last_synced_at":"2026-03-06T06:30:59.893Z","repository":{"id":137924140,"uuid":"172889699","full_name":"mizchi/use-react-redux-context","owner":"mizchi","description":"Alternative ReactRedux.connect by useContext for performance","archived":false,"fork":false,"pushed_at":"2019-03-13T05:10:54.000Z","size":257,"stargazers_count":43,"open_issues_count":0,"forks_count":3,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-08-19T22:02:07.684Z","etag":null,"topics":["react","react-hooks","react-redux","redux"],"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/mizchi.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-02-27T09:58:38.000Z","updated_at":"2022-11-21T03:27:52.000Z","dependencies_parsed_at":null,"dependency_job_id":"ead41dfe-aa1f-4518-a484-8a16885a3606","html_url":"https://github.com/mizchi/use-react-redux-context","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/mizchi/use-react-redux-context","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mizchi%2Fuse-react-redux-context","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mizchi%2Fuse-react-redux-context/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mizchi%2Fuse-react-redux-context/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mizchi%2Fuse-react-redux-context/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mizchi","download_url":"https://codeload.github.com/mizchi/use-react-redux-context/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mizchi%2Fuse-react-redux-context/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30164590,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-06T04:43:31.446Z","status":"ssl_error","status_checked_at":"2026-03-06T04:40:30.133Z","response_time":250,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["react","react-hooks","react-redux","redux"],"created_at":"2024-11-11T12:30:13.967Z","updated_at":"2026-03-06T06:30:59.560Z","avatar_url":"https://github.com/mizchi.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# use-react-redux-context\n\nAlternative `ReactRedux.connect()` by `useContext()` for performance.\n\n```bash\n# peer deps\n$ yarn add react react-dom redux react-redux\n$ yarn add use-react-redux-context\n# or npm install --save use-react-redux-context\nyarn add @types/react @types/redux @types/react-redux -D # for typescript\n```\n\n## Concept\n\n- Pre-defined connected React.Context with mapState (no own props)\n- Create bound actions by useCallback\n- Emit render by shallow equal comparation\n- TypeScript friendly\n\n## Why not `useContext(ReactReduxContext)` of `react-redux@6`\n\nJust `React.useContext(ReactReduxContext)` emits rendering by all state update. Pre-defined contexts and reducers are good for performance.\n\nAnd I don't know good `ownProps` usages on `ReactRedux.connect`.\n\n## How to use\n\nMinimum conceptual code\n\n```tsx\nimport { Provider as ContextProvider, Scope } from \"use-react-redux-context\";\nimport { Provider as ReactReduxProvider } from \"react-redux\";\n\nconst scope = new Scope();\nconst RootStateContext = scope.createContext(s =\u003e s);\nconst All = () =\u003e {\n  const all = useContext(RootStateContext);\n  return \u003cpre\u003e{JSON.stringify(all)}\u003c/pre\u003e;\n};\n\nconst store = createStore(reducer); // create your reducer\nconst el = (\n  \u003cReactReduxProvider store={store}\u003e\n    \u003cContextProvider scope={scope}\u003e\n      \u003cAll /\u003e\n    \u003c/ContextProvider\u003e\n  \u003c/ReactReduxProvider\u003e\n);\n```\n\nWorking code\n\n```tsx\nimport React, { useContext, useCallback, useEffect } from \"react\";\nimport ReactDOM from \"react-dom\";\nimport { createStore, combineReducers, Reducer } from \"redux\";\nimport { Provider as ReactReduxProvider } from \"react-redux\";\nimport { Provider as ContextProvider, Scope } from \"use-react-redux-context\";\n\n// Write your own reducer\n\ntype Foo = {\n  value: number;\n};\n\ntype RootState = {\n  foo: Foo;\n};\n\ntype IncrementAction = { type: \"increment\" };\ntype Action = IncrementAction;\n\nfunction foo(state: Foo = { value: 0 }, action: Action): Foo {\n  switch (action.type) {\n    case \"increment\": {\n      return { value: state.value + 1 };\n    }\n    default: {\n      return state;\n    }\n  }\n}\n\nfunction increment(): IncrementAction {\n  return { type: \"increment\" };\n}\n\n// Build connected React.Context\n\nconst scope = new Scope\u003cRootState\u003e(); // scope expand all contexts\nconst FooContext = scope.createContext((state, dispatch) =\u003e {\n  // this useCallback is under NewContext rendering hook context\n  const inc = useCallback(() =\u003e dispatch(increment()), []);\n  // emit render by shallow equal comparation\n  return { ...state.foo, inc };\n});\n\nfunction Counter() {\n  // This is React.useContext, not library function\n  const foo = useContext(FooContext);\n  return (\n    \u003cdiv\u003e\n      value: {foo.value}\n      \u003cbutton onClick={foo.inc}\u003e+1\u003c/button\u003e\n    \u003c/div\u003e\n  );\n}\n\nconst rootReducer: Reducer\u003cRootState\u003e = combineReducers({ foo });\nconst store = createStore(rootReducer);\n\nfunction App() {\n  return (\n    // Provider expand all scope's context\n    \u003cReactReduxProvider store={store}\u003e\n      \u003cContextProvider scope={scope}\u003e\n        \u003cCounter /\u003e\n      \u003c/ContextProvider\u003e\n    \u003c/ReactReduxProvider\u003e\n  );\n}\n\n// @ts-ignore\nReactDOM.render(\u003cApp /\u003e, document.querySelector(\".root\") as HTMLDivElement);\n```\n\n## API\n\n### `useDispatch`\n\nGet `store.dispatch` Function\n\n```tsx\nimport { useDispatch } from \"use-react-redux-context\";\nfunction X() {\n  const dispatch = useDispatch();\n  return (\n    \u003cbutton onClick={() =\u003e disptach({ type: \"my-action\" })}\u003emy action\u003c/button\u003e\n  );\n}\n```\n\n### `useBindAction`, `useBindActions`\n\nCreate function by action and actionMap (with `useCallback` and its `memoizedKeys`)\n\n```tsx\nimport { useBindAction, useBindActions } from \"use-react-redux-context\";\n\nconst A = scope.createContext(state =\u003e {\n  const inc = useBindAction(increment);\n  // with memoized keys\n  // const inc = useBindAction(() =\u003e ({type: 'increment', payload: state.value}), [state.value]);\n  return { ...state.a, inc };\n});\n\nconst B = scope.createContext(_state =\u003e {\n  const actions = useBindActions({ increment, decrement });\n  // alternative: with memoized keys map\n  // const actions = useBindActions({ increment, decrement }, { increment: [state.value] });\n\n  // recommend spreading for shallow equal\n  return { ...actions };\n});\n```\n\n## How to dev\n\n- `yarn build`: Build `index.js`\n- `yarn test`: Run jest\n- `yarn demo`: Start application server on `http://localhost:1234`\n- `yarn demo:deploy`: Deploy to netlify (need netlify account)\n\n## LICENSE\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmizchi%2Fuse-react-redux-context","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmizchi%2Fuse-react-redux-context","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmizchi%2Fuse-react-redux-context/lists"}