Redux:如何更新对象的单个属性

时间:2019-07-03 03:02:18

标签: redux react-redux

我当前的状态是这样的对象

{active: true, dateAdded: "02-07-2019", endEpoch: "02-07-2019", name: "pics"}

我的减速器

 export default (state = initial_state, actions) => {
    switch (actions.type) {
        case SUBREDDIT_SELECTED:
            {
                return { ...state,
                    selected_sub: actions.payload
                }
            }
            break;
        case EPOCH_CHANGED:
            {
                return { ...state,
                    selected_sub: actions.payload
                }
            }
            break;
        default:
            return state;
    }
}

如何更新状态的endEpoch属性?

1 个答案:

答案 0 :(得分:0)

您已经完成了自己想做的事情。要更新endEpoch,请执行

    case EPOCH_CHANGED:
        {
            return { ...state,
                endEpoch: actions.payload
            }
        }
        break;

传播state将所有道具保留在原处。并更改endEpoch