react-beautiful-dnd - 不变失败:找不到带有 id 的可删除条目

Mat*_*off 10 drag-and-drop list multiple-columns reactjs react-beautiful-dnd

我正在遵循一个react-beautiful-dnd教程,该教程使用react组件类而不是钩子。当我遇到这个错误时,我正在使用现代反应语法编写一个等效的程序。不变失败:找不到 id [column-1] 的可删除条目。我试图动态更新存储原始列索引的变量,以防止用户将任务拖动到较早的列。当我尝试在 onDragStart 函数内部使用 useState 挂钩时发生错误。我创建了一个显示问题的codeSandbox,非常感谢您的帮助。

小智 22

只需删除 index.jsx/index.tsx 文件上的 React.strictMode 标签

  • 你知道为什么 React.StrictMode 使 beautiful-dnd 无法使用吗? (5认同)
  • 难道就没有别的办法了吗? (2认同)

小智 9

您可以使用此自定义组件作为替代,而不是从“react-beautiful-dnd”导入“Dropable”表单:

import { useEffect, useState } from "react";
import { Droppable, DroppableProps } from "react-beautiful-dnd";
export const StrictModeDroppable = ({ children, ...props }: DroppableProps) => {
  const [enabled, setEnabled] = useState(false);
  useEffect(() => {
    const animation = requestAnimationFrame(() => setEnabled(true));
    return () => {
      cancelAnimationFrame(animation);
      setEnabled(false);
    };
  }, []);
  if (!enabled) {
    return null;
  }
  return <Droppable {...props}>{children}</Droppable>;
};
Run Code Online (Sandbox Code Playgroud)

TypeScript 版本的功劳归功于GitHub 上的GiovanniACamachoMeligy

  • 你值得更多的点赞 (2认同)

小智 7

尝试删除 React 的严格模式。