使用redux-persist,如何仅将对象的一部分作为默认状态的一部分返回

时间:2018-10-30 04:48:09

标签: javascript redux redux-persist

我接管的项目使用redux-persist设置初始状态。但是,我只希望更改初始状态的一部分。

export let defaultState = {
    devices: {},
    canBeDeleted: false,
    willBeDeleted: null,

};

我只希望在redux-persist补水时更新设备。

export default function (state = defaultState, action) {
    if (action.type === REHYDRATE) {
        let incoming = action.payload.device;
        console.log('checking actin.payload.device: ', action.payload.device);
        if (incoming) {
            return {
                ...state,
                ...action.payload.device.devices
            }
        }
        return {
            ...state
        }
    }

这导致未返回任何内容。我该如何指定我只希望返回设备数组,而其余的默认状态得到加载?

1 个答案:

答案 0 :(得分:0)

if (action.type === REHYDRATE) {
        // console.log('REHYDRATE: action.payload.device.devices: ', action.payload.device.devices)
        let incoming = action.payload.device;
        if (incoming) {
            return {
                ...state,
                devices: action.payload.device.devices
            }
        }
        return {
            ...state
        }
    }