【发布时间】:2021-03-18 05:55:48
【问题描述】:
我刚刚设置了一个函数来在使用 Tailwind CSS 时切换暗模式。这个错误突然出现,因为它像 5 分钟前一样工作正常。我没有更改任何代码,但突然出现此错误。
我正在使用 Next.js 和 Tailwind CSS。不是代码专家,只是学习和尝试。这是我使用 localStorage 的功能:
const [theme, setTheme] = useState(localStorage.theme);
const colorTheme = theme === 'dark' ? 'light' : 'dark';
useEffect(() => {
const root = window.document.documentElement;
root.classList.remove(colorTheme);
root.classList.add(theme);
localStorage.setItem('theme', theme);
}, [theme, colorTheme]);
return [colorTheme, setTheme];
}
希望在这里有一些光线。这对我来说很奇怪,因为它正在工作????
【问题讨论】:
标签: local-storage next.js tailwind-css