Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-dnd

Package Overview
Dependencies
Maintainers
5
Versions
140
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-dnd

Drag and Drop for React

  • 16.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2M
increased by1.61%
Maintainers
5
Weekly downloads
 
Created

What is react-dnd?

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.

What are react-dnd's main functionalities?

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}"}

Other packages similar to react-dnd

FAQs

Package last updated on 19 Apr 2022

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc