My question is which should I use? Can Redux Toolkit replace Redux core?
Redux used to be great but if you have tried none of them, I would highly recommend using Redux-Toolkit. The only case where I may want you to stick to redux is when you're using class-based components, where Redux Toolkit does have some boilerplate (like Redux) and you may miss out decent support.
However with functional components, Redux toolkit is like Redux on steroids.
Reason for using Redux toolkit:
A lot lesser boilerplate code is required compared to Redux.
Redux hooks like useSelector
and useDispatch
make things so short and easy to use. [This is not specific to Redux toolkit, however, highlighting it here as it is extremely helpful to consume these hooks in functional component and might be helpful for those who are completely new to redux]
You don't need to do manual thunk
setup as redux-toolkit comes with out of the box createAsyncThunk
which enables you to perform async operations in very hassle free way.
getState
is also very helpful in obtaining state variables across any of your actions or async operations.
Mutability might be considered as advantage or disadvantage, but if you're not too used to writing with spread operators, you might love this feature as well. Do straight assignments and let redux toolkit take care of mutability under the hoods.
current
can be used to log your state anywhere in case you want to debug and understand where things are going wrong. (Of course, Redux debugger tools are great as well)
Prebuilt templates: you might want to use npx create-react-app my-app --template redux-typescript
or if you're using it with next: yarn create next-app --example with-redux with-redux-app
. It gives you a setup ready redux toolkit
boiler plate and also contains a boiler plate of most important redux state management applications so that you could refer them to create your own slices very easily.
I have been using a lot of redux and initially it was a bit confusing but once you get a good command over redux toolkit and if you're using a lot of functional components, you probably would never go back to redux again.