{"id":16482504,"url":"https://github.com/f/react-hooks-todo-app","last_synced_at":"2025-03-16T18:31:40.991Z","repository":{"id":45754181,"uuid":"155117344","full_name":"f/react-hooks-todo-app","owner":"f","description":"A highly testable TodoList app that uses React hooks and Context.","archived":false,"fork":false,"pushed_at":"2018-11-04T12:32:22.000Z","size":54,"stargazers_count":188,"open_issues_count":2,"forks_count":23,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-02-27T12:09:07.842Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://1v4ovy3nnj.codesandbox.io/","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/f.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":"2018-10-28T21:27:21.000Z","updated_at":"2024-07-22T07:11:38.000Z","dependencies_parsed_at":"2022-07-30T13:18:01.535Z","dependency_job_id":null,"html_url":"https://github.com/f/react-hooks-todo-app","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/f%2Freact-hooks-todo-app","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/f%2Freact-hooks-todo-app/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/f%2Freact-hooks-todo-app/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/f%2Freact-hooks-todo-app/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/f","download_url":"https://codeload.github.com/f/react-hooks-todo-app/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243826788,"owners_count":20354220,"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":[],"created_at":"2024-10-11T13:11:02.934Z","updated_at":"2025-03-16T18:31:40.590Z","avatar_url":"https://github.com/f.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# React Hooks Todo App\n\n\u003e A trial to achieve a correct approach. Trying to get **rid off using Redux**, make **contexts more useful** with **useReducer** and make components **\"easy-to-test simple functions\"**.\n\n[![Edit react-usecontext-todo-app](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/github/f/react-hooks-todo-app/tree/master/)\n\n---\n\nA **highly decoupled**, **testable** TodoList app that uses React hooks.\n\nThis is a training repo to learn about new hooks feature of React and creating a testable environment.\n\n- **Zero-dependency**\n- **No** class components\n- Uses `Context` to share a **global state**\n- Uses `useReducer` to manage **state actions**\n- `useState` to create local state\n- Decoupled state logic (Actions)\n- Testable components (Uses Jest + Enzyme for tests)\n- Custom Hooks for **persisting state**.\n\nFor better approaches please open Pull Requests\n\n## Summary\n\n### 1. **Context**:\n\nThe main approach was to get rid off Redux and use **React Contexts** instead. With the composition of `useState`, `useContext` I created a global state. And passed it into a **custom hook** called `useTodos`. `useTodos` curries `useState` output and generates a state manager which will be passed into `TodoContext.Provider` to be used as a global state.\n\n```jsx\nfunction App() {\n  // create a global store to store the state\n  const globalStore = useContext(Store);\n\n  // `todos` will be a state manager to manage state.\n  const [state, dispatch] = useReducer(reducer, globalStore);\n\n  return (\n    // State.Provider passes the state and dispatcher to the down\n    \u003cStore.Provider value={{ state, dispatch }}\u003e\n      \u003cTodoList /\u003e\n      \u003cTodoForm /\u003e\n    \u003c/Store.Provider\u003e\n  );\n}\n```\n\n### 2. **The Reducer**:\n\nThe second approach was to seperate the main logic, just as the **actions** of Redux. But these are fully functional, every function returns whole state.\n\n```js\n// Reducer is the classical reducer that we know from Redux.\n// used by `useReducer`\nexport default function reducer(state, action) {\n  switch (action.type) {\n    case \"ADD_TODO\":\n      return {\n        ...state,\n        todos: [...state.todos, action.payload]\n      };\n    case \"COMPLETE\":\n      return {\n        ...state,\n        todos: state.todos.filter(t =\u003e t !== action.payload)\n      };\n    default:\n      return state;\n  }\n}\n```\n\n### 3. **State and Dispatcher**\n\nI reach out **state and dispathcer** of context using `useContext` and I can reach to the `actions`.\n\n```js\nimport React, { useContext } from \"react\";\nimport Store from \"../context\";\n\nexport default function TodoForm() {\n  const { state, dispatch } = useContext(Store);\n  // use `state.todos` to get todos\n  // use `dispatch({ type: 'ADD_TODO', payload: 'Buy milk' })`\n```\n\n### 4. **Persistence with custom hooks**:\n\nI created custom hooks to persist state on `localStorage`\n\n```js\nimport { useEffect } from \"react\";\n\n// Accepts `useContext` as first parameter and returns cached context.\nexport function usePersistedContext(context, key = \"state\") {\n  const persistedContext = localStorage.getItem(key);\n  return persistedContext ? JSON.parse(persistedContext) : context;\n}\n\n// Accepts `useReducer` as first parameter and returns cached reducer.\nexport function usePersistedReducer([state, dispatch], key = \"state\") {\n  useEffect(() =\u003e localStorage.setItem(key, JSON.stringify(state)), [state]);\n  return [state, dispatch];\n}\n```\n\nThe `App` function will be:\n\n```js\nfunction App () {\n  const globalStore = usePersistedContext(useContext(Store));\n\n  // `todos` will be a state manager to manage state.\n  const [state, dispatch] = usePersistedReducer(useReducer(reducer, globalStore));\n```\n\n### 5. **Everything is testable decoupled**:\n\nThe last but most important part of the approach is to make all the parts testable. They don't tie to eachother which makes me to write tests easily.\n\n## License\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ff%2Freact-hooks-todo-app","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ff%2Freact-hooks-todo-app","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ff%2Freact-hooks-todo-app/lists"}