5

当我尝试react-beautiful-dndnext.js(或通常与服务器端渲染一起使用)时,在重新排序项目并刷新页面后,我收到此错误:

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

这(取决于第一个):

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

我尝试使用resetServerContext()重置服务器上下文计数器,但它没有按预期工作。

4

4 回答 4

7

经过一番测试,我找到了解决方案。只需调用 resetServerContext() 服务器端:例如,在一个next.js页面中我简单地调用它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 : []}}

}
于 2020-10-07T10:54:11.250 回答
0

我使用 b-dnd 在 nextjs 页面的末尾使用了它。

export async function getServerSideProps(context) {
  resetServerContext()   
  return {props: { data : []}}
}

并在 next.config.js 中设置它。

module.exports = {
  reactStrictMode: true,
};

这行得通,但老实说,我并不完全理解其中的含义。

于 2022-02-13T15:49:18.730 回答
0

另一种解决方案:

const [isBrowser, setIsBrowser] = useState(false);

useEffect(() => {
  setIsBrowser(process.browser);
}, [])

function Home() {
  return(
   <>
    {isBrowser ? <DragDropContext> { /* all rbd code */ } </DragDropContext> : null}
   </>
  )
}
于 2022-01-08T17:45:44.180 回答
0

检查您是否正在使用 css-in-js 库。由于 nextjs 页面中的以下代码,我遇到了类似的问题:

<Draggable
 key={task.id}
 draggableId={task.id}
 index={index}
>
      {(provided, snapshot) => {
        return (
          <div
           className={styles.card}
           {...provided.draggableProps}
           {...provided.dragHandleProps}
           ref={provided.innerRef}
           // style={{
           //   ...provided.draggableProps.styles,
           // }}
           // The abpve code is commented due to React Hydration Error
           // Link: https://nextjs.org/docs/messages/react-hydration-error
          >
             .
             .
             .                 
          </div>
      );
 }}
</Draggable>

这是错误详细信息的链接:https ://nextjs.org/docs/messages/react-hydration-error

于 2022-02-28T13:42:24.723 回答