'Should I use Redux or Redux-Toolkit? [closed]
I am new in studying Redux. Then I know the existence about Redux-toolkit. My question is which should I use? Can Redux-toolkit replace Redux core?
Solution 1:[1]
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.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 hastle free way.getState
is also very helpful in obtaining state variables across any of youractions
orasync 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 letredux 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. (Ofcourse, Redux debugger tools are great as well)Prebuilt templates
you might want to usenpx 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.
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 |