gpt4 book ai didi

javascript - 在 NEXTjs 中使用 React-Moveable 时遇到问题 : TypeError: Cannot read property 'userAgent' of undefined

转载 作者:行者123 更新时间:2023-12-02 22:09:31 25 4
gpt4 key购买 nike

过去几个小时我一直在努力解决这个问题。我可以通过一个简单的 node.js 应用程序很好地使用react-moveable。当我尝试在 NEXTjs 应用程序中使用相同的模块时,它会抛出错误:TypeError: Cannot read property 'userAgent' of undefined

这是重复的问题: https://codesandbox.io/s/hungry-ramanujan-ttqh3?fontsize=14&hidenavigation=1&theme=dark

据我所知,我似乎正在尝试在服务器端渲染一些本应在客户端的内容。但不确定解决方案是什么......

感谢您的帮助!

最佳答案

这里的问题是该模块尚未准备好 SSR,并且它正在尝试读取 navigator.userAgent

您可以尝试将逻辑移动到另一个组件并使用 next/dynamic 动态导入它,并将 ssr 设置为 false:

import dynamic from 'next/dynamic';

// ...
// other stuff, imports, etc
// ...

const MoveableNoSSR = dynamic(() => import('./MoveableWrapper'), {
ssr: false,
loading: () => <div>Loading Moveable...</div>,
});

关于javascript - 在 NEXTjs 中使用 React-Moveable 时遇到问题 : TypeError: Cannot read property 'userAgent' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59603615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com