1

I'm trying to make a web app using Next JS 12.3.1 with draggable components using react-beautiful-dnd version 13.1.1. For now, I'm just trying to get a single vertical list of draggable components to work

import React, { useState } from "react";
import { DragDropContext, Droppable, Draggable } from "react-beautiful-dnd";

export default function BigOlList() {

const defaultList = ["A", "B", "C", "D", "E"];
// React state to track order of items
const [itemList, setItemList] = useState(defaultList);

// Function to update list on drop
const handleDrop = (droppedItem) => {
  // Ignore drop outside droppable container
  if (!droppedItem.destination) return;
  var updatedList = [...itemList];
  // Remove dragged item
  const [reorderedItem] = updatedList.splice(droppedItem.source.index, 1);
  // Add dropped item
  updatedList.splice(droppedItem.destination.index, 0, reorderedItem);
  // Update State
  setItemList(updatedList);
};

return (
  <div className="App">
    <DragDropContext onDragEnd={handleDrop}>
        <Droppable droppableId="list-container">
            {(provided) => (
                <div className="list-container" {...provided.droppableProps} ref={provided.innerRef}>
                    {itemList.map((item, index) => (
                        <Draggable key={item} draggableId={item} index={index}>
                            {(provided) => (
                                <div className="item-container"
                                {...provided.dragHandleProps}
                                {...provided.draggableProps}
                                ref={provided.innerRef}
                                >
                                    {item}
                                </div>
                            )}
                        </Draggable>
                    ))}
                    {provided.placeholder}
                </div>
            )}
        </Droppable>
        </DragDropContext>
  </div>
);
}

I tried following an example but none of my items are draggable. The grabbable cursor doesn't even show up when I hover over the boxes. It's just a normal pointer. I heard that resetServerContext needed to be called in _document.js, so I did that, but it didn't make a difference. The app still compiles with no issues, errors, or other indicators that anything is broken. Anyone have more experience with this library, or know of another more supported library?

Gabriel
  • 13
  • 3
  • This could be a style issue because I ran the code with my next.js app and it works. Also, 'resetServerContext' only needs to be called when you are using server side rendering. – Stephen Asuncion Oct 12 '22 at 02:17

1 Answers1

2

There are some issues with using react-beautiful-dnd with next.js.

Checkout this article or this.

If none of that works for you or you can't make changes like: // reactStrictMode: true, (I couldn't), I recommend to switch to DnD-kit. You can find documentation here.

It works fine for me.

Werthis
  • 557
  • 3
  • 15