简体   繁体   中英

Redux and redux toolkit having problem without using createslice

I wanted to ask can't we just simply use handwritten reducer in redux-toolkit as same as we use in redux js And then the rest of all thing I may keep according to tp redux-toolkit in store, but I don't how would I able to give initialState in configureStore please guide me I want to use same reducer function of redux handwritten instead of using createslice

Thank u

You can totally do that, and you can pass preloadedState into configureStore (although your hand-written reducer should have it's own "default state", as part of the method signature like function myReducer(state = initialState, action) { ... } ).

But I'd seriously ask you to reconsider. There might be a one-off case where a hand-written reducer has so specific logic that the hand-written reducer actually makes sense, but in 99% of the cases, createSlice reduces the amount of code (often by a factor of 2-4), and it also makes a lot of common errors like accidentally mutating state impossible (in a hand-written reducer, that is not allowed and considered a bug, in a createSlice reducer it is totally okay and will lead to an implicit immutable update).

What is the exact reason why you don't want to use createSlice ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM