{"id":17717740,"url":"https://github.com/mununki/practice-react-hooks","last_synced_at":"2026-01-20T07:02:10.577Z","repository":{"id":43590147,"uuid":"169527609","full_name":"mununki/practice-react-hooks","owner":"mununki","description":"A tiny example of the React Hooks - How to replace the React Redux","archived":false,"fork":false,"pushed_at":"2022-12-10T00:37:11.000Z","size":959,"stargazers_count":3,"open_issues_count":20,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2024-04-24T03:22:04.972Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/mununki.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-02-07T06:13:48.000Z","updated_at":"2024-04-24T03:22:04.973Z","dependencies_parsed_at":"2023-01-25T18:15:20.069Z","dependency_job_id":null,"html_url":"https://github.com/mununki/practice-react-hooks","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/mununki/practice-react-hooks","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mununki%2Fpractice-react-hooks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mununki%2Fpractice-react-hooks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mununki%2Fpractice-react-hooks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mununki%2Fpractice-react-hooks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mununki","download_url":"https://codeload.github.com/mununki/practice-react-hooks/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mununki%2Fpractice-react-hooks/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28597985,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-20T02:08:49.799Z","status":"ssl_error","status_checked_at":"2026-01-20T02:08:44.148Z","response_time":117,"last_error":"SSL_read: 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":[],"created_at":"2024-10-25T14:29:51.831Z","updated_at":"2026-01-20T07:02:10.554Z","avatar_url":"https://github.com/mununki.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Practice of the React Hooks - How to replace the React Redux\n\nThis is a tiny example using the React Hooks working like React Redux.\n\n1. `useHoodux()` = `useState()` + `useContext()`\n2. `useReducer()`\n\n## How to replace the Redux with React Hooks\n\n### Create the store (initValue + reducer)\n\n```javascript\n// App.js\n\nconst initValue = {\n  isLogin: false,\n  token: \"\"\n};\n\nconst reducer = (state, action) =\u003e {\n  switch (action.type) {\n    case \"setLogin\":\n      return { isLogin: action.payload };\n    case \"setToken\":\n      return { token: action.payload };\n    default:\n      throw new Error(\"wrong action.type\");\n  }\n};\n```\n\n### Wrap the top level component(eg. App.js) with Context\n\n```javascript\n// App.js\n\nconst MyContext = React.createContext();\n\nconst App = () =\u003e {\n  const [state, dispatch] = useReducer(reducer, initValue);\n\n  return (\n    \u003cMyContext.Provider value={{ state, dispatch }}\u003e\n      \u003cHeader /\u003e\n      \u003cLogIn /\u003e\n    \u003c/MyContext.Provider\u003e\n  );\n};\n```\n\n\u003e You can use the context wrapper component in higher order if you like.\n\n### Pull and use `state` and `dispatch`\n\n```javascript\n// Login.js\n\nconst { state, dispatch } = useContext(MyContext);\n\nconst handleLogin = () =\u003e {\n  dispatch({ type: \"setLogin\", payload: true });\n};\n\nreturn \u003cdiv\u003e{state.isLogin ? \"You are logged in\" : \"Not logged in\"}\u003c/div\u003e;\n```\n\n## Conclusion\n\n`useReducer` make a lot easier to pull and use the `state` and `dispatch` instead of using `connect()` in React Redux. If you're familiar with it, you know how it works. In React Redux,every single component, you want to use `state` or `dispatch`, needs to be connected. But with `useReducer`, you need to call it just once in component if you want it to be.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmununki%2Fpractice-react-hooks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmununki%2Fpractice-react-hooks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmununki%2Fpractice-react-hooks/lists"}