what is main difference between redux and redux toolkit, is saga required in redux toolkit? [closed]
Asked Answered
T

2

48

My question is which should I use? Can Redux Toolkit replace Redux core?

Thankyou answered 25/12, 2021 at 5:51 Comment(3)
Toolkit is Redux, its just an opinionated way of using it. It uses the library.Dado
FWIW there are plenty of other great state management libs (e.g. effector, recoil)Bacteriolysis
yes, I only know recoil and mobxThankyou
H
97

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:

  1. A lot lesser boilerplate code is required compared to Redux.

  2. 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]

  3. 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.

  4. getState is also very helpful in obtaining state variables across any of your actions or async operations.

  5. 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.

  6. 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)

  7. 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.

Hittite answered 25/12, 2021 at 6:0 Comment(3)
For reason #2, useSelector and useDispatch are available without the toolkit as well.Reorder
One correction: Redux Toolkit has nothing to do with 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
thanks, @Marseillaise and hyetigran for the correction. My sole purpose was just to highlight it as the asker of the question was new to redux so just wanted to highlight it.Hittite
M
19

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.

Marseillaise answered 25/12, 2021 at 9:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.