javascriptreactjsnext.jsserver-side-renderingreact-beautiful-dnd

How to fix `data-rbd-draggable-context-id` did not match. Server: "1" Client: "0"' with react-beautiful-dnd and next.js


When I try to use react-beautiful-dnd with next.js (or in general with server side rendering), after reorder items and refresh the page I get this error:

react-dom.development.js:88 Warning: Prop `data-rbd-draggable-context-id` did not match. Server: "1" Client: "0"

And this (that depends on the first one):

react-beautiful-dnd.esm.js:39 react-beautiful-dndA setup problem was encountered.> Invariant failed: Draggable[id: 1]: Unable to find drag handle

I try to use resetServerContext() to reset the server context counter, but it doesn't work as expected.


Solution

  • After some test i found a solution. Just call resetServerContext() server side.

    As an example, in a next.js page just call it in getServerSideProps

    import { GetServerSideProps } from "next";
    import React from "react";
    import { resetServerContext } from "react-beautiful-dnd";
    import { DndWrapper } from "../../components/DndWrapper";
    
    
    export default function App({ data }) {
    
        return <DragDropContext onDragEnd={onDragEnd}>...</DragDropContext>
    }
    
    export const getServerSideProps: GetServerSideProps = async ({ query }) => {
    
        resetServerContext()   // <-- CALL RESET SERVER CONTEXT, SERVER SIDE
    
        return {props: { data : []}}
    
    }