react使用dnd实现简单的拖拽排序

编程入门 行业动态 更新时间:2024-10-23 11:16:25

react使用dnd实现简单的<a href=https://www.elefans.com/category/jswz/34/1765858.html style=拖拽排序"/>

react使用dnd实现简单的拖拽排序

在react项目中使用dnd实现一个简单的拖拽排序功能

首先简单的介绍一下dnd:

React-DnD是一组React实用程序,可帮助您构建复杂的拖放界面,同时保持组件之间的耦合。

1、DndProvider组件为您的应用程序提供React-DnD功能。必须通过backendc参数将其注入后端,但是也可以将其注入window对象。

backend后端是React-DnD中非常好的一种设计方法。可以理解为具体拖拽的实现方式

2、userDrag声明拖动源

userDrag用于将当前组件用作拖动源的钩子。

3、userDrop声明放置源

useDrop用于使用当前组件作为放置目标的钩子。

下面是具体实现拖拽功能的组件代码

import React, { useRef } from 'react';
import { useDrop, useDrag } from 'react-dnd';
// dnd拖拽排序
export default (props: any) => {const { moveRow, index, children } = props;const ref: any = useRef(null);const accept = 'DragDropBox';const [{ isOver, dropClassName }, drop] = useDrop({accept,collect: (monitor) => {const { index: dragIndex } = (monitor.getItem() || {}) as any;if (dragIndex === index) {return {};}return {isOver: monitor.isOver(),dropClassName: dragIndex < index ? ' drop-over-downward' : ' drop-over-upward',};},drop: (item: any) => {moveRow(item.index, index);},});const [, drag] = useDrag({type: accept,item: { index },collect: (monitor: any) => ({isDragging: monitor.isDragging(),}),});const changeRef = drag(drop(ref));return (<div// @ts-ignoreref={changeRef}key={index + 1}className={`${isOver ? dropClassName : ''}`}><span key={index}>{children}</span></div>);
};

使用时

import { DndProvider } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';
import ReactDndDragSort from './drag';
import { useCallback} from 'react';const moveRow = useCallback((dragIndex, hoverIndex) => {const newItems = [...textValue];newItems.splice(hoverIndex, 1, ...newItems.splice(dragIndex, 1, newItems[hoverIndex]));onChange?.(newItems);},[textValue],);<DndProvider backend={HTML5Backend}><WSpace size={'large'}>{textValue.map((item: string, index: number) => {return (<div key={index}><ReactDndDragSort index={index} moveRow={moveRow}><div>需要拖拽的模块</div></ReactDndDragSort></div>);})}</WSpace>
</DndProvider>

更多推荐

react使用dnd实现简单的拖拽排序

本文发布于:2024-02-27 08:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1705930.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:拖拽   简单   react   dnd

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!