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
anduseDispatch
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 boxcreateAsyncThunk
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 readyredux 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.
useSelector
, useDispatch
or connect
. There is no difference between using Redux or Redux Toolkit in regards to React. That is the separate react-redux
package you have to use in both cases. The difference between the two is the amount and safety of non-React-related code. –
Marseillaise Redux Toolkit is the official recommendation for all Redux code you write since 2019. See the Redux Style guide on this and Why Redux Toolkit is How To Use Redux Today.
I would recommend you start learning Redux by following the official Redux "Essentials" tutorial from the Redux homepage - and that will teach you Redux Toolkit from the beginning.
© 2022 - 2024 — McMap. All rights reserved.