【发布时间】:2020-12-14 11:06:35
【问题描述】:
我正在使用 react 组件开发下一个 js,一切正常,但我在 react-location-picker 中收到 ReferenceError: window is not defined。
【问题讨论】:
标签: javascript node.js reactjs next.js
我正在使用 react 组件开发下一个 js,一切正常,但我在 react-location-picker 中收到 ReferenceError: window is not defined。
【问题讨论】:
标签: javascript node.js reactjs next.js
因为next.js 将在服务器端和客户端执行代码。 window is not defined 在服务器端执行代码时会发生。您可以使用dynamic 导入包,使其仅在客户端运行。这是给你的solution。
import dynamic from 'next/dynamic';
const reactLocation = dynamic(() => import('react-location-picker'), {
ssr: false,
});
【讨论】:
试试
const reactLocation = require("react-location-picker")
【讨论】: