reactjs - 将新数组对象添加到 react redux reducer 中的嵌套数组和 store 更新中的数组,但不添加 react 组件
问题描述
我是 React 和 redux 的新手。我有一个任务,比如添加工作流,在工作流内部有任务。我可以将新的工作流对象添加到状态数组中......但是在添加新任务时,它会直接添加到工作流数组中,而不是任务数组中。这是我的数组结构
const initialState =
{ WORKFLOWS:
[{ id: 1, Title: 'testdata1', Status: 'Completed',
Tasks: [{ id: 1, Title: 'Task1', Content: 'Test Content', Status: 'Pending' }, { id: 2, Title: 'Task2', Content: 'Test Content2', Status: 'Pending' }] },
{ id: 2, Title: 'testdata2', Status: 'Pending',
Tasks:[ { id: 2, Title: 'Task1', Content: 'Test Content', Status: 'Pending' }] }], workflowid: 3, isloading: false};
这是我的减速机
export const reducer = (state, action) => {
状态 = 状态 || 初始状态;
if (action.type === ADD_WORKFLOW) {
return {
...state,
WORKFLOWS: [...state.WORKFLOWS, { id: state.workflowid++, Title: 'Workflow' + state.workflowid, Status: 'Pending', Tasks : [] }]
//WORKFLOWS: [...state.WORKFLOWS, [{ id: state.workflowid++, Title: 'Workflow' + state.workflowid, Status: 'Pending' }]]
};
}
if (action.type === EDIT_WORKFLOWTITLE) {
const index = state.WORKFLOWS.map(item => item.id).indexOf(action.itemid);
console.log(index);
return {
...state, WORKFLOWS: [...state.WORKFLOWS.map(todo =>
todo.id === action.itemid ? { ...todo, Title: action.itemtitle } : todo
)]
}
}
if (action.type === EDIT_WORKFLOWSTATUS) {
return {
...state, WORKFLOWS: [...state.WORKFLOWS.map(todo =>
todo.id === action.itemid ? { ...todo, Status: (todo.Status === 'Pending') ? 'Completed' : 'Pending' } : todo
)]
}
}
if (action.type === DELETE_WORKFLOW) {
const index = state.WORKFLOWS.map(item => item.id).indexOf(action.payload);
return { ...state, WORKFLOWS: [...state.WORKFLOWS.slice(0, index).concat(state.WORKFLOWS.slice(index + 1))] };
}
if (action.type === ADD_TASK) {
console.log(state.WORKFLOWS);
return {
...state,
WORKFLOWS: [...state.WORKFLOWS,
{
Tasks: [...state.WORKFLOWS[1], { id: state.workflowid++, Title: 'Workflow' + state.workflowid, Content: 'Workflow' + state.workflowid, Status: 'Pending' }]
}]
};
}
return state;
};
我正面临 add_task 操作的问题,这是单击添加任务时的输出
0: {id: 1, Title: "testdata1", Status: "Completed", Tasks: Array(2)}
1: {id: 2, Title: "testdata2", Status: "Pending", Tasks: Array(1)}
2: {Tasks: Array(1)}
我也试过下面的代码数组得到更新,但视图没有更新。
if (action.type === ADD_TASK) {
let obj = { id: state.workflowid++, Title: 'Workflow' + state.workflowid, Content: 'Workflow' + state.workflowid, Status: 'Pending' };
let newarray = [];
newarray = state.WORKFLOWS;
newarray[1].Tasks.push(obj);
console.log(newarray);
console.log(state.WORKFLOWS)
return {...state, WORKFLOWS: newarray};
}
解决方案
希望这可以帮助。
if (action.type === ADD_TASK) {
console.log(state.WORKFLOWS);
return {
...state,
WORKFLOWS: [...state.WORKFLOWS,
{ id: state.workflowid++, Title: 'Workflow' + state.workflowid, Content: 'Workflow' + state.workflowid, Status: 'Pending',
Tasks: [ { id: 3, Title: 'Task3', Content: 'Test Content', Status: 'Pending' }]
}]
};
}
推荐阅读
- python - django 中的 JsonResponse 和 HttpResponse 有什么区别
- html - 如何覆盖父框架的元视口?
- java - 混合内容:“url”处的页面是通过 HTTPS 加载的,但请求的脚本不安全,内容必须通过 HTTPS 提供
- php - 如何根据当前时间对多维数组进行排序?
- selenium - org.openqa.selenium.WebDriverException:未知错误:无法读取未定义的属性“defaultView”
- oracle12c - TNS-12505:TNS:listener 在关闭其中一个数据库实例后当前不知道连接描述符中给出的 SID
- r - 添加以列值为条件的附加行
- javascript - 提交表单,尽管字段是必填项-> 使用 Bootstrap Checkout 示例
- sql-server - 按列生成具有树路径的视图
- javascript - 如何下载当前计算机/磁盘上的文件?注意:我有一个其他计算机可以连接的本地 IP - node.js