Should you replace Redux with Hooks?
TL; DR; No. Redux now has state and dispatch hooks
The first question I asked when I saw the useReducer() hook was, “Do I need Redux anymore?” I am sure a million other React devs asked the same. So I went and did some experimentation to replace a Redux application with hooks.
The plot was simple.
- Write a reducer and associated actions
- Make its state available to the whole application via a useContext hook.
- Bind your actions to this reducer’s dispatch function.
What did I gain?
No more mapStateToProps(), mapDispatchToProps() and connect(). My container components looked like React components! It was so easy to reason about.
But then I realized that I could have multiple reducers to manage other parts of the state. Now I needed a “combineReducers()” function. I probably needed a withMiddleware() function one just in case.
You can see it in action here. https://codesandbox.io/s/0y8q3yq92l. But the more I looked at it, it looked just like Redux sans the prop connect boilerplate.
And then, Redux introduced hooks support with two very simple functions, useSelector() and useDispatch(). There was nothing new in my code anymore.
In summary, there is no need to reinvent the wheel. Just use Redux. The initial setup is simple and a one time thing. All you need to now learn is how Redux reducers and actions work.