Reactjs WebPackageError:ReferenceError:未定义文档-netlify生成
我在netlify上构建项目时出错(WebPackageError:ReferenceError:document未定义)。在开发工作中表现出色强> 我看到用JSDOM解决这个问题是可能的,但我已经尝试过了,但我无法使它正确Reactjs WebPackageError:ReferenceError:未定义文档-netlify生成,reactjs,build,gatsby,Reactjs,Build,Gatsby,我在netlify上构建项目时出错(WebPackageError:ReferenceError:document未定义)。在开发工作中表现出色 我看到用JSDOM解决这个问题是可能的,但我已经尝试过了,但我无法使它正确 import React from "react"; import "../css/DarkMode.css"; const DarkMode = () => { let clicked
import React from "react";
import "../css/DarkMode.css";
const DarkMode = () => {
let clickedClass = "clicked";
const body = document.body;
const lightTheme = "light";
const darkTheme = "dark";
let theme;
if (localStorage) {
theme = localStorage.getItem("theme");
}
if (theme === lightTheme || theme === darkTheme) {
body.classList.add(theme);
} else {
body.classList.add(lightTheme);
}
const switchTheme = (e) => {
if (theme === darkTheme) {
body.classList.replace(darkTheme, lightTheme);
e.target.classList.remove(clickedClass);
localStorage.setItem("theme", "light");
theme = lightTheme;
} else {
body.classList.replace(lightTheme, darkTheme);
e.target.classList.add(clickedClass);
localStorage.setItem("theme", "dark");
theme = darkTheme;
}
};
return (
<button
className={theme === "dark" ? clickedClass : ""}
id="darkMode"
className={"btnDarkMode"}
onClick={(e) => switchTheme(e)}
></button>
);
};
export default DarkMode;
从“React”导入React;
导入“./css/DarkMode.css”;
常数暗模式=()=>{
让clickedClass=“单击”;
const body=document.body;
const lightTheme=“light”;
const darkTheme=“黑暗”;
让主题;
if(本地存储){
theme=localStorage.getItem(“主题”);
}
如果(主题===光主题| |主题===暗主题){
body.classList.add(主题);
}否则{
body.classList.add(lightTheme);
}
const switchTheme=(e)=>{
如果(主题===暗主题){
body.classList.replace(暗主题、光主题);
e、 target.classList.remove(单击类);
setItem(“主题”、“灯光”);
主题=轻主题;
}否则{
body.classList.replace(lightTheme,darkTheme);
e、 target.classList.add(点击class);
setItem(“主题”、“暗”);
主题=黑暗主题;
}
};
返回(
切换主题(e)}
>
);
};
导出默认暗模式;
您想实现主题切换的想法完全错误
useffect()
hook中使用它。(第一页之后)document.body
等等)。此外,这些操作大大降低了代码/应用程序的性能,因为它们会给浏览器带来巨大的成本,这就是React如此之快的原因(除其他外)
在React应用程序中操纵真实DOM可能会导致严重的警告和警告,可能会阻止(按需刷新/重新呈现新内容)
总之,您的代码在gatsby develope
下工作,而不是在gatsby build
下工作,因为gatsby develope
由浏览器处理gatsby build
id在节点服务器中编译,其中显然没有窗口
或文档
,因为它们甚至还没有创建
简单的解决方法是在以下情况下包装代码(使用全局对象):
if (typeof window !== `undefined`) { // or typeof document !== 'undefined'
// your code that uses global objects here
}
但正如我所说,它可能会阻止水化反应
要在盖茨比中添加主题,您可以选择使用插件()或使用基于React的方法
有用资源: