liveBook · Manning

Drag and drop

As explained in React Quickly, Second Edition

Drag and drop is an interaction technique in which a user moves a pointer-selected item to a destination and releases it there.

Drag and drop lets users move a selected item to a destination and release it there. In a task manager, it reprioritizes steps by moving them within a task list. section 13.0

Implement the interaction by combining event handling with list updates:

  • Use HTML5 drag-and-drop with draggable, dragstart, dragover, dragenter, dragleave, and drop events. section 13.5
  • Alternatively, build the interaction from mousedown, mousemove, and mouseup events. section 13.5
  • Represent valid drop positions explicitly, including before the first item and after the last item. section 13.5
  • Update the reducer so a step can move to an arbitrary position, while accounting for moves earlier or later in the list. section 13.5

Moving a task step

A reducer action can describe the operation as moveStepTo({ taskId, step, position }). Render an insertion target between every pair of steps, attach onDrop to those targets, and use onDragEnter, onDragLeave, and onDragOver to make them valid drop zones. section 13.5

Figure 13.4 You can drag steps in the list for easier reprioritization.
13-04

Visualizes steps being dragged within a task list for reprioritization. section 13.0

How React Quickly, Second Edition covers it

How deep it goes
Details event choices, drop boundaries, insertion targets, and reducer updates, while acknowledging the implementation remains difficult.
Examples
Demonstrates draggable task steps, insertion targets, and arbitrary repositioning within a list.
What it assumes
Assumes HTML and JavaScript familiarity for advanced work while relying on React-supported event handlers.
What you can do after
Build it from scratch as a React interaction that moves task steps to arbitrary list positions.
How it connects
Connects HTML5 drag-and-drop, mouse events, React events, reducers, and immutable array updates.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of React Quickly, Second Edition. The section links point to the parts of the book it draws on.