当前位置:首页 > React

react怎么实现拖拽排序

2026-01-27 13:03:12React

实现拖拽排序的基本思路

在React中实现拖拽排序通常需要借助第三方库,如react-dndreact-beautiful-dnd。核心逻辑是通过监听拖拽事件,动态更新数据顺序,并触发重新渲染。

使用react-dnd实现

安装依赖库:

npm install react-dnd react-dnd-html5-backend

创建可拖拽的列表项组件:

import { useDrag, useDrop } from 'react-dnd';

const Item = ({ id, text, index, moveItem }) => {
  const [{ isDragging }, drag] = useDrag({
    type: 'ITEM',
    item: { id, index },
    collect: (monitor) => ({
      isDragging: monitor.isDragging(),
    }),
  });

  const [, drop] = useDrop({
    accept: 'ITEM',
    hover: (draggedItem) => {
      if (draggedItem.index !== index) {
        moveItem(draggedItem.index, index);
        draggedItem.index = index;
      }
    },
  });

  return (
    <div
      ref={(node) => drag(drop(node))}
      style={{
        opacity: isDragging ? 0.5 : 1,
        padding: '8px',
        margin: '4px',
        border: '1px solid #ccc',
      }}
    >
      {text}
    </div>
  );
};

创建列表容器组件:

react怎么实现拖拽排序

import { DndProvider } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';

const List = () => {
  const [items, setItems] = useState([
    { id: 1, text: 'Item 1' },
    { id: 2, text: 'Item 2' },
    { id: 3, text: 'Item 3' },
  ]);

  const moveItem = (fromIndex, toIndex) => {
    const newItems = [...items];
    const [movedItem] = newItems.splice(fromIndex, 1);
    newItems.splice(toIndex, 0, movedItem);
    setItems(newItems);
  };

  return (
    <DndProvider backend={HTML5Backend}>
      <div>
        {items.map((item, index) => (
          <Item
            key={item.id}
            id={item.id}
            text={item.text}
            index={index}
            moveItem={moveItem}
          />
        ))}
      </div>
    </DndProvider>
  );
};

使用react-beautiful-dnd实现

安装依赖库:

npm install react-beautiful-dnd

创建可排序列表:

react怎么实现拖拽排序

import { DragDropContext, Droppable, Draggable } from 'react-beautiful-dnd';

const List = () => {
  const [items, setItems] = useState([
    { id: '1', content: 'Item 1' },
    { id: '2', content: 'Item 2' },
    { id: '3', content: 'Item 3' },
  ]);

  const handleDragEnd = (result) => {
    if (!result.destination) return;

    const newItems = Array.from(items);
    const [reorderedItem] = newItems.splice(result.source.index, 1);
    newItems.splice(result.destination.index, 0, reorderedItem);
    setItems(newItems);
  };

  return (
    <DragDropContext onDragEnd={handleDragEnd}>
      <Droppable droppableId="items">
        {(provided) => (
          <div {...provided.droppableProps} ref={provided.innerRef}>
            {items.map((item, index) => (
              <Draggable key={item.id} draggableId={item.id} index={index}>
                {(provided) => (
                  <div
                    ref={provided.innerRef}
                    {...provided.draggableProps}
                    {...provided.dragHandleProps}
                    style={{
                      padding: '8px',
                      margin: '4px',
                      border: '1px solid #ccc',
                      ...provided.draggableProps.style,
                    }}
                  >
                    {item.content}
                  </div>
                )}
              </Draggable>
            ))}
            {provided.placeholder}
          </div>
        )}
      </Droppable>
    </DragDropContext>
  );
};

性能优化建议

对于大型列表,使用react-windowreact-virtualized进行虚拟滚动。在拖拽过程中避免不必要的重新渲染,可以使用React.memo优化子组件。

移动端兼容性

react-beautiful-dnd默认不支持移动端触摸事件,需要额外配置。react-dnd通过react-dnd-touch-backend可以支持触摸事件:

npm install react-dnd-touch-backend

使用时替换backend:

import { TouchBackend } from 'react-dnd-touch-backend';

// 在组件中
<DndProvider backend={TouchBackend} options={{ enableMouseEvents: true }}>
  {/* 你的组件 */}
</DndProvider>

标签: 拖拽react
分享给朋友:

相关文章

react native 如何

react native 如何

安装 React Native 开发环境 确保系统已安装 Node.js(建议版本 16 或更高)。通过以下命令安装 React Native CLI 工具: npm install -g reac…

电脑如何安装react

电脑如何安装react

安装 Node.js 和 npm React 项目依赖 Node.js 环境,需先安装 Node.js(自带 npm)。访问 Node.js 官网 下载 LTS 版本,运行安装程序并完成配置。安装后,…

vue拖拽容器实现

vue拖拽容器实现

实现 Vue 拖拽容器的基本方法 使用 HTML5 原生拖拽 API 通过 draggable 属性和拖拽事件(dragstart、dragend、dragover、drop)实现基础拖拽功能。适用于…

react 如何引入jquery

react 如何引入jquery

引入 jQuery 到 React 项目 在 React 项目中引入 jQuery 可以通过多种方式实现,但需要注意 React 和 jQuery 操作 DOM 的方式可能冲突,因此建议仅在必要时使用…

react如何拓展

react如何拓展

React 拓展方法 使用高阶组件(HOC) 高阶组件是一种复用组件逻辑的方式,通过接收一个组件并返回一个新组件实现功能拓展。例如,为组件添加日志功能: function withLogging…

react如何检测

react如何检测

检测 React 应用的方法 使用 PropTypes 进行类型检查 PropTypes 是 React 内置的类型检查工具,用于验证组件的 props 是否符合预期。在开发环境中,PropTypes…