React Hooks Todo App
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".
A highly decoupled, testable TodoList app that uses React hooks.
This is a training repo to learn about new hooks feature of React and creating a testable environment.
- Zero-dependency
- No class components
- Uses
Context
to share a global state - Uses
useReducer
to manage state actions useState
to create local state- Decoupled state logic (Actions)
- Testable components (Uses Jest + Enzyme for tests)
- Custom Hooks for persisting state.
For better approaches please open Pull Requests
Summary
1. Context:
The 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.
2. The Reducer:
The second approach was to seperate the main logic, just as the actions of Redux. But these are fully functional, every function returns whole state.
3. State and Dispatcher
I reach out state and dispathcer of context using useContext
and I can reach to the actions
.
4. Persistence with custom hooks:
I created custom hooks to persist state on localStorage
The App
function will be:
5. Everything is testable decoupled:
The 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.