18
loading...
This website collects cookies to deliver better user experience
# yarn
yarn add react-beautiful-dnd
# npm
npm install react-beautiful-dnd --save
App.js
file or inside of the index.js
file. I chose the index.js
file because it keeps the App component cleaner as you might add things to your application.<Droppable />
and them wrap the area you want to accept draggables inside of it: droppableId
<Droppable />
. It <Droppable />
you have.<Droppable />
must be wrapped in a callback function with the arguments "provided" and "snapshot" given.provided.innerRef
- This must be attached to the outer most DOM element you have. required
provided.droppableProps
- This is an Object within the provided argument that contains information the library uses to function. Belongs on the same element you added innerRef
required
provided.placeholder
- This is used to tell the droppable not to shrink down when you are dragging things around, from my understanding. Place this inside of the element you attached innerRef
to<Draggable />
is used to drag things onto and off of <Droppable />
s. Every <Draggable />
needs to be inside a <Droppable />
. In order for the user to interact with a <Draggable />
, each one needs a Drag Handle. On top of each one needing a unique draggableId
, each one needs an index to show its position in the list. Additionally, any children of a <Draggable />
is wrapped in a callback function, same as the <Droppable />
. Here is an example implementation: <Draggable />
for each object with its own information, index, and draggableId
. I then import this function into my App.js
and place it inside of my <Droppable />
's callback function: <DragDropContext />
a onDragEnd
function. This function will handle reordering the list when you drag items to a new position. When you drag something, you get a result
object returned, we can pass this result
to our own drag end function to make it work. First of all, since we are needing to re-render the list after draggables have been moved, we need to set our list to state: onDragEnd
function that looks like this: result
object: onDragEnd
function we have made is as follows: