[英]ThemeProvider: "theme" prop is required. → React.js
I created a Custom Hook to switch themes, but I'm facing this problem and I don't exactly what could it be.我创建了一个自定义挂钩来切换主题,但我正面临这个问题,我不知道它到底是什么。
Basically, when I try to change the theme, it works perfectly.基本上,当我尝试更改主题时,它可以完美运行。 But, after refreshing the page, it doesn't stay with the correct one and gives me this error.
但是,刷新页面后,它不会保留正确的页面并给我这个错误。
You can reproduce this issue, cloning the Edite GitHub repository and follow the guide to set up the services.您可以复制此问题,克隆Edite GitHub 存储库并按照指南设置服务。
Note: see that src is the root注意:看到 src 是根
hooks/useThemeSwitcher.js
import { useState, useEffect } from 'react';
function useThemeSwitcher(key, initialTheme) {
const [theme, setTheme] = useState(
() => {
let storagedTheme = localStorage.getItem(key);
storagedTheme = JSON.parse(storagedTheme) || initialTheme;
}
);
useEffect(() => {
localStorage.setItem(key, JSON.stringify(theme));
}, [key, theme]);
return [theme, setTheme];
}
export default useThemeSwitcher;
App.js
function App() {
// Current theme state (light/dark);
const [theme, setTheme] = useThemeSwitcher('theme', dark);
const toggleTheme = () => {
setTheme(theme.title === 'dark' ? light : dark)
}
return (
<ThemeProvider theme={theme}>
<Global />
{/* ...components */}
<ToolbarLeft toggleTheme={toggleTheme} />
</ThemeProvider>
);
}
components/Switch/index.js
// Components (styles)
import { CustomSwitch } from './switch.styles';
function Switch({ isToggled, onSwitch }) {
return (
<CustomSwitch>
<input
type="checkbox"
checked={isToggled}
onChange={onSwitch}
/>
<span />
</CustomSwitch>
)
}
export default Switch;
components/Toolbar/Left/index.js
// Components (styles)
import { LeftContainer } from '../toolbar.styles';
// Components (children)
import ToolsList from './tools';
function ToolbarLeft({ toggleTheme }) {
return (
<LeftContainer>
<ul>
<ToolsList toggleTheme={toggleTheme} />
</ul>
</LeftContainer>
)
}
export default ToolbarLeft;
components/Toolbar/Left/tools.js
function ToolsList({ toggleTheme }) {
// Access and set the theme colors
const { title } = useContext(ThemeContext);
return (
<>
{/* ...components */}
{/* Theme switcher */}
<Switch
isToggled={title === 'dark'}
onSwitch={toggleTheme}
</Switch>
</>
)
}
The problem is that you are not returning storagedTheme
inside your useThemeSwitcher
hook.问题是您没有在
useThemeSwitcher
挂钩中返回storagedTheme
。
So you could change your useThemeSwitcher.js
to something like this:因此,您可以将
useThemeSwitcher.js
更改为以下内容:
import { useState, useEffect } from "react";
function useThemeSwitcher(key, initialTheme) {
const [theme, setTheme] = useState(() => {
let storagedTheme = localStorage.getItem(key);
return JSON.parse(storagedTheme) || initialTheme;
});
useEffect(() => {
localStorage.setItem(key, JSON.stringify(theme));
}, [key, theme]);
return [theme, setTheme];
}
export default useThemeSwitcher;
I had the same issue.我遇到过同样的问题。 I had not imported the theme from my components folder.
我没有从我的组件文件夹中导入主题。 So I had to:
所以我不得不:
import { lightTheme } from "./components/Themes";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.