Security News
Maven Central Adds Sigstore Signature Validation
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
react-dnd
Advanced tools
The react-dnd package is a set of React utilities to help you build complex drag and drop interfaces while keeping your components decoupled. It provides a way to use drag and drop features in React by turning any React component into a draggable or a droppable area.
Draggable Components
This feature allows you to turn any component into a draggable item. The useDrag hook is used to make components draggable and to monitor their state.
{"import { useDrag } from 'react-dnd';\n\nfunction DraggableComponent() {\n const [{isDragging}, dragRef] = useDrag({\n type: 'BOX',\n collect: monitor => ({\n isDragging: !!monitor.isDragging(),\n }),\n });\n\n return (\n <div ref={dragRef} style={{ opacity: isDragging ? 0.5 : 1 }}>\n Drag Me\n </div>\n );\n}"}
Droppable Areas
This feature allows you to define areas where draggable items can be dropped. The useDrop hook is used to handle the drop events and to determine the state of the drop targets.
{"import { useDrop } from 'react-dnd';\n\nfunction DroppableComponent() {\n const [{canDrop, isOver}, dropRef] = useDrop({\n accept: 'BOX',\n drop: () => ({ name: 'DroppableComponent' }),\n collect: monitor => ({\n isOver: monitor.isOver(),\n canDrop: monitor.canDrop(),\n }),\n });\n\n return (\n <div ref={dropRef} style={{ backgroundColor: isOver ? 'green' : 'white' }}>\n {canDrop ? 'Release to drop' : 'Drag a box here'}\n </div>\n );\n}"}
Custom Drag Layer
This feature allows you to create a custom drag layer that can be used to display a custom preview of the draggable item. The useDragLayer hook provides information about the item being dragged and its position.
{"import { useDragLayer } from 'react-dnd';\n\nfunction CustomDragLayer() {\n const { isDragging, item, currentOffset } = useDragLayer((monitor) => ({\n item: monitor.getItem(),\n currentOffset: monitor.getSourceClientOffset(),\n isDragging: monitor.isDragging(),\n }));\n\n if (!isDragging) {\n return null;\n }\n\n return (\n <div style={{ position: 'fixed', pointerEvents: 'none', left: currentOffset.x, top: currentOffset.y }}>\n {/* Render custom drag layer here */}\n </div>\n );\n}"}
react-beautiful-dnd is a higher-level abstraction built on top of the lower-level primitives provided by react-dnd. It offers a more opinionated but simpler API and focuses on vertical and horizontal lists. It provides beautiful and accessible drag and drop for lists with React.
react-sortable-hoc is a set of higher-order components to turn any list into an animated, touch-friendly, sortable list. It's more focused on list sorting capabilities and less on general drag and drop scenarios compared to react-dnd.
dnd-kit is a modern drag and drop toolkit for React that is designed to be more performant and flexible than react-dnd. It provides hooks and utilities to build complex drag and drop interfaces and supports multiple backends, including touch and keyboard.
Drag and drop for React with full DOM control.
Currently works on top of HTML5 drag-and-drop API, but is moving towards a swappable implementation. Support for touch events is on the roadmap but not currently implemented.
If your company uses React DnD and would like to speed up its development, consider sponsoring the project. I'm currently doing it in the spare time but I would be happy to work on it part-time. Drop me an email at dan.abramov@me.com. Exciting new features take time.
Demo contains a variety of things you can implement with this library, including:
You can do much more, but these examples will help you get started!
preventDefault
anything to start working;enter
and leave
as you'd expect instead of doing it for every child node;render()
;We plan to follow SemVer after 1.0. Before 1.0, minor version bumps may contain breaking changes. Breaking changes will be documented in the Upgrade Guide.
Complete API reference is available here.
If you don't feel comfortable diving into examples source code just yet, you can start with the walkthrough.
Have you played with live demo yet? Here's the source code for it.
To try it locally, clone the project and run:
npm install
npm start
open http://localhost:8080/
Examples use ES6 syntax via Babel transpiler. You can still use React DnD in an ES5 codebase, but if you copy code from examples, make sure you translate it to ES5 where needed.
The library can be used separately (dist/ReactDND.min.js
) or with a bundler such as Webpack or Browserify.
npm install --save react-dnd
This library is a React port of an API, parts of which were originally written by Andrew Kuznetsov.
A lot of recent progress is due to Nathan Hutchison's contributions and effort.
1.0.0-alpha (2015-04-20)
FAQs
Drag and Drop for React
We found that react-dnd demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.