【发布时间】:2020-05-19 12:59:38
【问题描述】:
首先,这里有一个 CodeSandbox:https://codesandbox.io/s/todolist-reducer-0y3mb?file=/src/context/TodosReducer.js
问题在于,即使我将带有新数组的有效负载传递给减速器,状态也没有更新。
我正在像这样重新排列数组:
const rearrange = (array, source, destination) => {
const newArray = [...array];
const newItem = array[source];
newArray.splice(source, 1);
newArray.splice(destination, 0, newItem);
return newArray;
};
然后调度它:
const endDrag = newArray => {
dispatch({
type: "endDrag",
payload: newArray
});
};
然后我在reducer中返回新的状态。
return {
...state,
todos: action.payload
};
记录有效载荷显示它是好的,但状态仍然没有更新。我正在使用 react beautiful dnd,所以可能与它有关。
【问题讨论】: