好,林建設一個簡單的待辦事項應用學習之用,和Im面對,我解決不了的警告:警告:flattenChildren(...):遇到兩個孩子使用相同的密鑰
當我檢查了待辦事項作爲完成,錯誤被拋出。我試圖將鍵設置爲每個元素的索引,警告停止,但列表更新時呈現重複的元素。
我的繼承人組成:
import React from "react";
import TodoTask from "./TodoTask";
const TodoList = ({todos, onTaskCheck}) => {
function renderList() {
return (
todos.map((todo) => {
return <TodoTask key={todo.id} todo={todo} onTaskCheck={onTaskCheck} />
})
);
}
return (
<div>
<h1 className="title is-4">ToDo List</h1>
<ul className="task-list">
{renderList()}
</ul>
</div>
);
};
export default TodoList;
繼承人的減速機:
export default function TodoReducers(state = [], action) {
switch(action.type) {
case "LIST_TODOS":
return [
{id: 1, description: "Task 1", isCompleted: true},
{id: 2, description: "Task 2", isCompleted: true},
{id: 3, description: "Task 3", isCompleted: true},
{id: 4, description: "Task 4", isCompleted: false},
{id: 5, description: "Task 5", isCompleted: false}
];
case "CHECK_TODO":
return [...state, Object.assign({}, action.payload, action.payload.isCompleted = true)];
default:
return state;
}
}
The rest of the code is here (GitHub)
謝謝,遺憾的英語不好!
謝謝!我認爲我仍然對Redux的工作方式感到困惑=) – user3344570
那麼,每當一個動作被觸發時,你就編輯你的狀態並返回它。我認爲你確實理解這一部分。你只是錯誤地編輯了你的狀態。你不必使用數組解構本身('[... state,{}]'部分)。只要你避免副作用,你可以將它寫下來,但是你可以隨心所欲地寫。 – stinodes