简体   繁体   English

Redux 和 redux 工具包在不使用 createlice 的情况下出现问题

[英]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我想问一下,我们不能像在 redux js 中一样简单地在 redux-toolkit 中使用手写 reducer 然后我可以根据商店中的 tp redux-toolkit 保留所有的东西,但我不知道如何我能否在 configureStore 中给出 initialState 请指导我我想使用与 redux 手写相同的减速器功能而不是使用 createlice

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) { ... } ).你完全可以这样做,你可以将preloadedState传递给configureStore (虽然你的手写reducer应该有它自己的“默认状态”,作为方法签名的一部分,比如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).可能存在一次性的情况,即手写 reducer 具有如此特定的逻辑,以至于手写 reducer 实际上是有意义的,但在 99% 的情况下, createSlice减少了代码量(通常是 2- 4),并且它还使许多常见错误,例如不可能意外改变状态(在手写reducer中,这是不允许的并被认为是错误,在createSlice reducer中完全可以,并且会导致隐式不可变更新)。

What is the exact reason why you don't want to use createSlice ?您不想使用createSlice的确切原因是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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