{"id":21241188,"url":"https://github.com/icyjoseph/context-hook-provider","last_synced_at":"2026-05-01T03:34:06.511Z","repository":{"id":35014249,"uuid":"183110322","full_name":"icyJoseph/context-hook-provider","owner":"icyJoseph","description":"A pattern proposal to move away from connect using hooks","archived":false,"fork":false,"pushed_at":"2022-12-10T17:15:28.000Z","size":459,"stargazers_count":1,"open_issues_count":22,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-23T02:11:18.709Z","etag":null,"topics":["react-context","react-hooks","react-redux","redux","redux-provider"],"latest_commit_sha":null,"homepage":"https://nondescript-ship.surge.sh/","language":"JavaScript","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/icyJoseph.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"License.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-04-23T23:27:06.000Z","updated_at":"2022-06-22T08:46:36.000Z","dependencies_parsed_at":"2023-01-15T12:15:42.023Z","dependency_job_id":null,"html_url":"https://github.com/icyJoseph/context-hook-provider","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/icyJoseph%2Fcontext-hook-provider","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/icyJoseph%2Fcontext-hook-provider/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/icyJoseph%2Fcontext-hook-provider/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/icyJoseph%2Fcontext-hook-provider/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/icyJoseph","download_url":"https://codeload.github.com/icyJoseph/context-hook-provider/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243681005,"owners_count":20330155,"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":["react-context","react-hooks","react-redux","redux","redux-provider"],"created_at":"2024-11-21T00:54:56.249Z","updated_at":"2026-05-01T03:34:06.480Z","avatar_url":"https://github.com/icyJoseph.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# context-hook-provider\n\nA pattern proposal.\n\nThe aim is to stop using connect. Instead, we should `useContext` and create hooks that select from the redux store.\n\nDemo [here](https://nondescript-ship.surge.sh/).\n\n## Install\n\nAs usual:\n\n```bash\nyarn add context-hook-provider\n```\n\nor\n\n```bash\nnpm install context-hook-provider\n```\n\n## Usage\n\nAs usual, we create a redux store and pass it to the Provider.\n\n```jsx\nimport React from \"react\";\nimport ReactDOM from \"react-dom\";\nimport { createStore } from \"redux\";\nimport { Provider } from \"context-hook-provider\";\n\n// using createStore directly from redux\nconst store = createStore(reducer); // the regular redux store\n\nReactDOM.render(\n  \u003cProvider store={store}\u003e\n    \u003cApp /\u003e\n  \u003c/Provider\u003e,\n  document.getElementById(\"root\")\n);\n```\n\nWhere Provider is:\n\n```jsx\nimport React, { createContext, useState, useEffect } from \"react\";\n\n// create a Context\nexport const State = createContext();\n\nexport function Provider({ store, children }) {\n  const [state, updateState] = useState(store.getState());\n\n  const listener = () =\u003e updateState(store.getState());\n\n  useEffect(() =\u003e {\n    const unsub = store.subscribe(listener);\n    return () =\u003e unsub();\n  }, []);\n\n  return (\n    \u003cState.Provider value={{ state, dispatch: store.dispatch }}\u003e\n      {children}\n    \u003c/State.Provider\u003e\n  );\n}\n```\n\nThen we can easily consume the state. Say for example that `\u003cApp\u003e`, has a child called `\u003cCounter\u003e`.\n\n```jsx\nimport React, { useContext } from \"react\";\nimport { State } from \"context-hook-provider\";\n\nexport function Counter() {\n  const {\n    state: { count }\n  } = useContext(State);\n  return \u003cdiv\u003e{count}\u003c/div\u003e;\n}\n```\n\nFurthermore, you can make a hook on the `count`.\n\n```jsx\nimport { useContext } from \"react\";\n\nexport function useCount() {\n  const { state } = useContext(State);\n  return state.count;\n}\n```\n\nAnd re-do the Counter.\n\n```jsx\nimport React from \"react\";\nimport useCount from \"./\";\n\nexport function Counter() {\n  const count = useCount();\n  return \u003cdiv\u003e{count}\u003c/div\u003e;\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ficyjoseph%2Fcontext-hook-provider","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ficyjoseph%2Fcontext-hook-provider","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ficyjoseph%2Fcontext-hook-provider/lists"}