reactjs - 不可变地更新redux中的对象数组
问题描述
我在这里改变状态,但我不想!我所有不改变状态的尝试都返回了语法错误,所以我转向了这里。
这是我的 redux 数据结构:
controls: (array)[
0:
id: "e06c5fbf-6d57-4f5b-a601-bfc4ad265def"
status: 'complete'
files: []
1:
id: "e06c5fbf-6d57-4f5b-a601-bfc4ad265def"
status: 'complete'
files: []
...
]
// 和我的减速器
export default function frameworkReducer(state = initialState, action) {
case RECEIVE_CONTROL_STATUS_UPDATE:
const index = action.payload.index;
const newState = {...state};
newState.controls[index] = { ...state.controls[index], status: action.payload.value };
return newState
解决方案
这对我有用:
// 减速器
let index = action.payload.index
let controls = [...state.controls];
controls[index] = {...controls[index], status: action.payload.value};
return {...state, controls}
推荐阅读
- c# - “退出构造函数时不可为空的事件必须包含非空值”
- django - 如何为仅创建者 django 设置编辑/删除权限
- java - 如果客户端不在 while 循环内,则读取消息失败
- xamarin.forms - Xamarin.Forms 与 FreshMVVM。向 FreshBasePageModel 类添加新属性
- javascript - React Google Maps API (ReferenceError: google is not defined)
- python - 安装热图的问题
- c++ - 将对象传递给 << 重载会改变 C++ 中对象中的数据
- magento2 - Magento 2 将数量变量发送到产品页面
- html - 渲染后如何获取元素的高度?
- vue.js - 上升的元素拼贴画(我该怎么做)