首页 > 解决方案 > 在 redux 存储更改后,React 组件没有重新渲染和更新

问题描述

重现此问题的沙箱链接: https ://codesandbox.io/s/redux-component-store-issue-kegqo

在此处输入图像描述

标签: reactjsreact-redux

解决方案


沙盒链接问题已修复: https ://codesandbox.io/s/fix-redux-component-store-issue-j7oxb?file=/src/redux/reducers/userReducer.js

const userReducer = (state=initialState, actions) => {
    switch(actions.type) {
        case UPDATE_USERS:
            return {...state, users: [...actions.payload.users]}
        default:
            return {...state}
    }
}

在此处输入图像描述

在 react reducer 中,对于数组使用[...Array],对于对象使用 {...object}


推荐阅读