我使用 zustand 创建一个带有待办事项数组的全局状态,我有添加、删除和切换每个待办事项完成/撤消的功能。
我还使用 Dnd Kit 拖放每个 ToDo 组件并在全局上下文中对它们重新排序,为此我需要以这种方式在 zustand 存储中创建一个 reorderTodo 函数:
const handleDragEnd = (event: any) => {
const { active, over } = event;
if (active.id !== over.id) {
const newIndex = filteredTodos.findIndex(
(todo) => todo.id === over.id
);
setFilteredTodos((filteredTodos) => {
const oldIndex = filteredTodos.findIndex(
(todo) => todo.id === active.id
);
return arrayMove(filteredTodos, oldIndex, newIndex);
});
reorderTodo(active.id, newIndex);
}
};
import { create } from "zustand";
export const useTodosStore = create(() => ({
todos: [
{ id: 1, text: "Learn TypeScript", done: true },
{ id: 2, text: "Try immer", done: false },
{ id: 3, text: "Tweet about it", done: false },
],
addTodo: (text: string, done: boolean) => {
useTodosStore.setState((state) => ({
todos: [
...state.todos,
{ id: state.todos.length + 1, text, done: done },
],
}));
},
toggleTodo: (id: number) =>
useTodosStore.setState((state) => ({
todos: state.todos.map((todo) =>
todo.id === id ? { ...todo, done: !todo.done } : todo
),
})),
removeTodo: (id: number) =>
useTodosStore.setState((state) => ({
todos: state.todos.filter((todo) => todo.id !== id),
})),
reorderTodo: (id: number, position: number) =>
useTodosStore.setState((state) => {
const todos = [...state.todos];
const todo = todos.find((todo) => todo.id === id);
if (!todo) return;
const idx = todos.indexOf(todo);
todos.splice(idx, 1);
todos.splice(position, 0, todo);
return { todos };
}),
}));
它在 VSCode 中给了我一个大的打字稿错误:
Argument of type '(state: { todos: { id: number; text: string; done: boolean; }[]; addTodo: (text: string, done: boolean) => void; toggleTodo: (id: number) => void; removeTodo: (id: number) => void; reorderTodo: (id: number, position: number) => void; }) => { ...; } | undefined' is not assignable to parameter of type '{ todos: { id: number; text: string; done: boolean; }[]; addTodo: (text: string, done: boolean) => void; toggleTodo: (id: number) => void; removeTodo: (id: number) => void; reorderTodo: (id: number, position: number) => void; } | Partial<...> | ((state: { ...; }) => { ...; } | Partial<...>)'.
Type '(state: { todos: { id: number; text: string; done: boolean; }[]; addTodo: (text: string, done: boolean) => void; toggleTodo: (id: number) => void; removeTodo: (id: number) => void; reorderTodo: (id: number, position: number) => void; }) => { ...; } | undefined' is not assignable to type '(state: { todos: { id: number; text: string; done: boolean; }[]; addTodo: (text: string, done: boolean) => void; toggleTodo: (id: number) => void; removeTodo: (id: number) => void; reorderTodo: (id: number, position: number) => void; }) => { ...; } | Partial<...>'.
但是我可以使用 yarn dev 在开发模式下运行应用程序,当我想使用 yarn build 构建应用程序时,问题就出现了,它在控制台中给了我这个错误,因此我也无法部署该应用程序在 Netlify/vercel 中
如何创建重新排序函数来修复此错误?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
问题是 set 方法需要返回状态,并且您的代码在 setState 中返回
undefined:if (!todo) return;。在这一行中,您可以将代码更改为if (!todo) return { todos };