React DnD Multi-Backend

9.0.0 · active · verified Tue Apr 21

This library provides a multi-backend system compatible with React DnD, enabling applications to seamlessly switch between different drag-and-drop backends, such as HTML5 and Touch. It is currently at version 9.0.0, maintaining an active release cadence that often aligns with major React and `dnd-core` updates. A key differentiator is its ability to allow a single `DndProvider` to manage multiple backends simultaneously, simplifying the implementation of responsive drag-and-drop interfaces that need to work across various input methods (e.g., mouse for desktop, touch for mobile). The library facilitates a robust and flexible approach to handling diverse drag-and-drop interactions within React applications without extensive manual switching logic.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates setting up `DndProvider` with `MultiBackend` and `HTML5ToTouch` for combined HTML5 and touch support.

import React from 'react';
import { DndProvider } from 'react-dnd';
import MultiBackend, { HTML5ToTouch } from 'react-dnd-multi-backend';
import { TouchBackend } from 'react-dnd-touch-backend';
import { HTML5Backend } from 'react-dnd-html5-backend';

interface MyDraggableProps {
  name: string;
}

const MyDraggable: React.FC<MyDraggableProps> = ({ name }) => {
  // In a real app, use useDrag hook here
  return (
    <div style={{ padding: '10px', margin: '5px', border: '1px solid gray' }}>
      {name}
    </div>
  );
};

const App: React.FC = () => {
  const options = {
    backends: [
      { backend: HTML5Backend },
      { backend: TouchBackend, options: { enableMouseEvents: true } },
    ],
  };

  return (
    <DndProvider backend={MultiBackend} options={options}>
      <h1>Drag and Drop with Multi-Backend</h1>
      <p>Try dragging these elements with mouse or touch.</p>
      <MyDraggable name="Item 1" />
      <MyDraggable name="Item 2" />
    </DndProvider>
  );
};

export default App;

view raw JSON →