javascript - 处理状态更新对象

标签 javascript redux

我正在使用 redux 来处理应用程序的状态,但我发现在嵌套时很难正确更新状态。

正如你所看到的,我在我的 reducer 中使用了 ...state,但是当我只需要更新子对象的键并保留其余状态时,有没有办法使用它?请参阅下面的示例

// initial state
state = {
    isFetching: true,
    isUpdating: false,
    content: {
        key: 'content',
        otherkey: 'content' // Keep this one
    }
}

// returned new state
{
    ...state,
    content: {
        key: 'thing I only want to update'
    }
}

操作

export function requestUser() {
    return  {
        type: REQUEST_USER
    };
}

export function receiveUser(data) {
    return {
        type: RECEIVE_USER,
        payload: data
    };
}

export function fetchUser(userhash) {  
    return function (dispatch) {
        dispatch(requestUser);  

        return new Promise(resolve => setTimeout(resolve, 500)).then(() => {
            const data = {
                status: 200,
                data: {
                    firstName: 'Neal',
                    lastName: 'Van der Valk',
                    email: 'email@outlook.com', 
                    hash: 'zea7744e47747851',
                    permissions: {
                        'admin': true,
                        'results': true,
                        'database': true,
                        'download': true,
                        'property': true,
                        'departments': true,
                        'users': true,
                        'devices': true,    
                        'integrations': true,
                    },
                    notifications: {
                        'daily': true,
                        'weekly': false
                    }
                }
            };

            dispatch(receiveUser(data));
        });
    };
}

reducer

const INITIAL_STATE = {
    isFetching: true,
    isUpdating: false,
    content: null
};

export default function(state = INITIAL_STATE, action) {
    switch (action.type) {
    case REQUEST_USER:
        return {
            ...state, 
            isFetching: true
        };

    case RECEIVE_USER:
        return {
            ...state, 
            isFetching: false,
            content: action.payload.data
        };

最佳答案

您可以尝试Object.assign()

此示例显示了用法。

{
    ...state,
    content: Object.assign({}, state.content, { 
        key: 'thing I only want to update'
    }
}

您还可以使用相同的扩展运算符...

    var state = {
        isFetching: true,
        isUpdating: false,
        content: {
            key: 'content',
            otherkey: 'content' // Keep this one
        }
    };

    var newState = {
       ...state,
       content: { 
         ...state.content, 
         key: 'thing I only want to update'
       }
    }
    console.log(newState);

关于javascript - 处理状态更新对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41224926/

相关文章:

javascript - Slick carousel - 使用 3 张幻灯片和 autoPlay=true 会导致 slider-nav 表现得像有 5 张幻灯片

javascript - 在页面刷新时,解构 Prop 在 componentDidMount 中给出未水化的值

javascript - 使用 CellMeasurer react 虚拟表无法正确计算高度

javascript - React.js Redux reducer 不插入项目

javascript - 开 Jest - 如何测试 console.error 被调用?

javascript - 确定当前是否正在左键单击按钮(按住单击)?

javascript - 有多少项目就重复 .then()

javascript - 从 async.each 发送到两个不同的函数

javascript - ReduxReducer中Action.type未定义错误

Javascript 对象而不是 Redux?