Registry / web-framework / react-dnd-html5-backend

react-dnd-html5-backend

JSON →
library16.0.1jsnpmunverified

react-dnd-html5-backend provides an essential implementation for drag-and-drop functionality within the React DnD ecosystem, leveraging the native HTML5 Drag and Drop API. This backend handles the intricacies of browser-specific drag and drop events and interactions, abstracting them into React DnD's unified API for consistent use across applications. The current stable version is 16.0.1, and the package actively aligns with the release cadence of the core react-dnd library. Its key differentiator lies in its robust reliance on the browser's built-in HTML5 drag-and-drop capabilities, offering a performant and widely supported solution. Recent major updates, notably in versions 15 and 16, have focused on migrating to ESM-only modules, deprecating the older Decorators API in favor of a Hooks-based approach, and improving compatibility with modern JavaScript environments and Node.js runtimes.

npm install react-dnd-html5-backend
INSTALL
IMPORT
SIG · REACT-DND-HTML5-BA
R
react-dnd-html5-backend
web-frameworkjavascriptv16.0.1
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

HTML5Backend
✓ import { HTML5Backend } from 'react-dnd-html5-backend'
✗ const HTML5Backend = require('react-dnd-html5-backend')
Package is ESM-only since v16.0.0; CommonJS require() will fail in most modern Node.js environments without transpilation.
DndProvider
✓ import { DndProvider } from 'react-dnd'
While react-dnd-html5-backend provides the backend, the DndProvider component itself is imported from the main 'react-dnd' package to wrap your application.
BackendFactory
✓ import type { BackendFactory } from 'react-dnd'
For type-checking in TypeScript, BackendFactory type can be useful, typically imported from 'react-dnd' rather than directly from the backend package.

This quickstart demonstrates a basic sortable list using React DnD with the HTML5 Backend, showcasing DndProvider, useDrag, and useDrop hooks to enable drag-and-drop reordering of cards.

import { HTML5Backend } from 'react-dnd-html5-backend'; import { DndProvider, useDrag, useDrop } from 'react-dnd'; import React from 'react'; import ReactDOM from 'react-dom'; const ItemTypes = { CARD: 'card' }; interface CardProps { id: number; text: string; moveCard: (id: number, to: number) => void; index: number; } const Card: React.FC<CardProps> = ({ id, text, moveCard, index }) => { const ref = React.useRef<HTMLDivElement>(null); const [, drop] = useDrop<CardProps, unknown, unknown>({ accept: ItemTypes.CARD, hover(item, monitor) { if (!ref.current) { return; } const dragIndex = item.index; const hoverIndex = index; if (dragIndex === hoverIndex) { return; } const hoverBoundingRect = ref.current?.getBoundingClientRect(); const hoverMiddleY = (hoverBoundingRect.bottom - hoverBoundingRect.top) / 2; const clientOffset = monitor.getClientOffset(); const hoverClientY = clientOffset!.y - hoverBoundingRect.top; if (dragIndex < hoverIndex && hoverClientY < hoverMiddleY) { return; } if (dragIndex > hoverIndex && hoverClientY > hoverMiddleY) { return; } moveCard(item.id, hoverIndex); item.index = hoverIndex; } }); const [{ isDragging }, drag] = useDrag({ type: ItemTypes.CARD, item: { id, index, text }, collect: (monitor) => ({ isDragging: monitor.isDragging() }) }); drag(drop(ref)); return ( <div ref={ref} style={{ border: '1px dashed gray', padding: '0.5rem 1rem', marginBottom: '.5rem', backgroundColor: 'white', cursor: 'move', opacity: isDragging ? 0.4 : 1 }} > {text} </div> ); }; const Container: React.FC = () => { const [cards, setCards] = React.useState([ { id: 1, text: 'Write a cool JS library' }, { id: 2, text: 'Make it generic enough' }, { id: 3, text: 'Write some docs' }, { id: 4, text: 'Profit' } ]); const moveCard = React.useCallback((id: number, to: number) => { const dragIndex = cards.findIndex(card => card.id === id); const dragCard = cards[dragIndex]; const newCards = [...cards]; newCards.splice(dragIndex, 1); newCards.splice(to, 0, dragCard); setCards(newCards); }, [cards]); return ( <div> {cards.map((card, i) => ( <Card key={card.id} id={card.id} text={card.text} index={i} moveCard={moveCard} /> ))} </div> ); }; ReactDOM.render( <DndProvider backend={HTML5Backend}> <Container /> </DndProvider>, document.getElementById('root') );
Debug
Known issues
breakingThe package transitioned to ESM-only modules starting with v16.0.0. This means CommonJS 'require()' statements will no longer work, requiring projects to adopt ES Modules for imports or use a bundler that handles ESM correctly.
fix
Update import statements from `const MyComponent = require('pkg')` to `import { MyComponent } from 'pkg'` and ensure your build setup (e.g., webpack, Rollup, Node.js environment) supports ES Modules.
affects: >=16.0.0
breakingThe Decorators API was completely removed in v15.0.0 and replaced by the Hooks API. Existing components using decorators (e.g., @DragSource, @DropTarget) must be refactored to use `useDrag` and `useDrop` hooks.
fix
Refactor components to utilize React Hooks (`useDrag`, `useDrop`, `useMonitor`) for drag-and-drop functionality instead of class decorators. Refer to the official React DnD documentation for Hooks API examples.
affects: >=15.0.0
gotchaAttempting to specify a `begin` method within the `useDrag::spec` configuration will throw a developer exception. This method is intentionally disallowed in the Hooks API.
fix
Remove any `begin` method from your `useDrag` hook specifications. The `item` and `collect` functions should encapsulate the necessary logic.
affects: >=14.0.2
gotchaEarlier versions had issues with drag-and-drop operations within iframes or child windows. While a fix was introduced in v14.0.3, ensure you are on a recent version if encountering such problems.
fix
Upgrade to `react-dnd-html5-backend@14.0.3` or newer to benefit from fixes related to iframe and child window interactions.
affects: <14.0.3
gotchav15.0.0 was initially published as ESM-only, but quickly reverted in v15.0.1 due to issues, temporarily restoring CommonJS/ESM format. The permanent transition to ESM-only occurred in v16.0.0. This history can cause confusion if relying on specific minor versions.
fix
Avoid `v15.0.0`. If targeting v15, use `v15.0.1` or newer for dual CJS/ESM support, or upgrade to `v16.0.0+` and fully embrace ESM.
affects: 15.0.0
Errors
Common errors & fixes
ReferenceError: require is not defined
Attempting to use CommonJS 'require()' to import the package in an ESM context (Node.js module with `"type": "module"` or modern bundler config).
fix
Change `const HTML5Backend = require('react-dnd-html5-backend');` to `import { HTML5Backend } from 'react-dnd-html5-backend';`.
TypeError: (0 , react_dnd__WEBPACK_IMPORTED_MODULE_1__.DndProvider) is not a function
Incorrect import or configuration of the DndProvider from 'react-dnd', or the 'backend' prop is missing/invalid.
fix
Ensure `DndProvider` is correctly imported from 'react-dnd' and passed a valid backend, e.g., `<DndProvider backend={HTML5Backend}>...</DndProvider>`.
TypeError: 'useDrag' cannot be called inside a class component.
Trying to use React Hooks (like `useDrag` or `useDrop`) inside a class component after the Decorators API was removed.
fix
Convert your class component to a functional component and use the `useDrag` and `useDrop` hooks. Alternatively, if you need class components, use an older `react-dnd` version that supports decorators (pre-v15.0.0).
Module not found: Can't resolve 'react-dnd-html5-backend'
The package is not installed, or there's a typo in the import path.
fix
Run `npm install react-dnd-html5-backend react-dnd` or `yarn add react-dnd-html5-backend react-dnd` and double-check the import statement for correct spelling.
Upgrade
Version history
16.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
react-dnd-html5-backend — npm install react-dnd-html5-backend · libregistry