【问题标题】:Update state object's property with Redux使用 Redux 更新状态对象的属性
【发布时间】:2017-12-26 19:43:30
【问题描述】:

我有这样的初始化状态:

  let initState = {
    checkBoxValues:{
        react: false,
        angular: false,
        vue: true,
        python: false,
        java: true
    }
  };

还有我的减速机:

export default (state = initState, action) => {
    switch(action.type){

    case "SET_CHECKBOX":
        let {name,checked}= action;

        return  {...state, checkBoxValues: ?????? }

    default:
        return state

    }
}

如何更新我的状态? 例如名称为“react”且选中为真。

  let {name,checked}= action;

【问题讨论】:

    标签: javascript reactjs redux react-redux state


    【解决方案1】:

    你可以像这样在没有突变的情况下更新它

    return  {...state, checkBoxValues: {...state.checkBoxValues,[name]: checked } }
    

    【讨论】:

      猜你喜欢
      • 2019-12-07
      • 2018-07-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-20
      相关资源
      最近更新 更多