react-beautiful-dnd组件报Unable to find draggable with id

一、问题现象

项目中使用react-beautiful-dnd组件实现可拖拽,但拖了1次后可能会出现拖拽异常(元素拖不动),打开控制台会发现有报错

二、解决方案

给Draggable组件和其下方的div添加了key就正常了,以下是我自己简单写的一个demo,可供参考

cpp 复制代码
import { useState } from 'react'
import { DragDropContext, Droppable, Draggable } from 'react-beautiful-dnd';
import './App.css'

const mocklist = [
  {
    label: 'item11',
    id:'aa',
    value: 'aa',
    color: 'red'
  },
  {
    label: 'item22',
    value: 'bb',
    id:'bb',
    color: 'blue'
  },
  {
    label: 'item33',
    value: 'cc',
    id:'cc',
    color: 'yellow'
  },
  {
    label: 'item44',
    value: 'dd',
    id:'dd',
    color: 'pink'
  },
  {
    label: 'item55',
    value: 'ee',
    id:'ee',
    color:'green'
  },
]
function App() {

  const [list,setList] =useState(mocklist);

  // 重新排序-更新列表
  const reorder = (list, startIndex, endIndex) => {
    const result = [...list];
    const [removed] = result.splice(startIndex, 1);
    result.splice(endIndex, 0, removed);
    return result;
  };

  // 拖拽结束
  const onDragEnd =(result)=>{
    if (!result.destination) {
      return;
    }
    if (result.destination.index === result.source.index) {
      return;
    }
    const newList = reorder(
      list,
      result.source.index,
      result.destination.index,
    );
    setList(newList)
  }

  return (
    <div>
      <DragDropContext onDragEnd={onDragEnd}>
      <div>
        <Droppable droppableId="list" key="list">
          {provided => (
            <div ref={provided.innerRef} {...provided.droppableProps}>
              {
              list.map((item, index) => {
                return (
                  <Draggable draggableId={item.id} index={index} key={item.id}>
                    {provided => (
                      <div
                        ref={provided.innerRef}
                        key={item.value}
                        {...provided.draggableProps}
                        {...provided.dragHandleProps}
                      >
                        <div style={{ marginBottom: 10,backgroundColor:item.color }}>
                          {item.label}
                        </div>
                      </div>
                    )}
                  </Draggable>
                );
              })
            }
              {provided.placeholder}
            </div>
          )}
        </Droppable>
      </div>
    </DragDropContext>
    </div>
  )
}

export default App

三、备注

注意:貌似这个组件在vite脚手架起的项目中会有问题(元素不能被拖拽),亲测在react脚手架ok

相关推荐
Stirner1 小时前
React 史诗级漏洞: SSR Server Action 协议导致服务器远程代码执行
react.js·架构·next.js
Dragon Wu2 小时前
ReactNative Expo 使用总结(基础)
javascript·react native·react.js
qq_229058013 小时前
react的3中请求
前端·react.js·前端框架
Debroon4 小时前
从零开始手写ReAct Agent
前端·javascript·react.js
开发者小天4 小时前
React中的受控组件示例
前端·javascript·react.js
初遇你时动了情4 小时前
react native实战项目 瀑布流、菜单吸顶、grid菜单、自定义背景图、tabbar底部菜单、轮播图
javascript·react native·react.js
黛色正浓5 小时前
【React】极客园案例实践-发布文章模块
前端·react.js·前端框架
开发者小天5 小时前
react的组件库antd design表格多选,删除的基础示例
前端·javascript·react.js
渴望成为python大神的前端小菜鸟5 小时前
react 面试题
前端·react.js·前端框架·react·面试题
吃炸鸡的前端5 小时前
Vite创建react项目
前端·react.js·前端框架