【问题标题】:Redux spread InitialState patternRedux 传播 InitialState 模式
【发布时间】:2021-04-10 01:34:50
【问题描述】:

最近我偶然发现了每个 reducer 都如下所示的代码库。我猜他们正在传播 initialState 以在 reducer 初始化时摆脱一些“剩余”嵌套状态,但这有意义吗?

const initialState = {...}

const reducer = (state = {...initialState}, action) => {
  ...
}

【问题讨论】:

  • 不需要使用扩展运算符,反正redux不会覆盖初始值

标签: redux react-redux reducers redux-reducers


【解决方案1】:

如果你的 reducer 没有违反不变性原则,传播 initialState 没有任何意义,因为 reducer 无论如何都会在每个操作上返回一个新状态(例如使用上述传播运算符)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 2016-08-25
    相关资源
    最近更新 更多