reactjs - 错误:[ThemeProvider] 请将您的主题道具设为普通对象
问题描述
你好我正在尝试使用情感主题,但是我遇到了这个错误,我无法解决
代码:
const [theme, setTheme] = usePersistedState('theme', light);
const toggleTheme = () => {
setTheme(theme.title === 'light' ? dark : light);
};
return (
<ThemeProvider theme={theme}>
<div className="App">
<button onClick={toggleTheme}>a</button>
</div>
</ThemeProvider>
);
我的钩子:
function usePersistedState(key, initialState) {
const [state, setState] = useState(() => {
const storageValue = localStorage.getItem(key);
if (storageValue) {
return JSON.parse(storageValue);
} else {
return initialState;
}
});
useEffect(() => {
localStorage.setItem(key, JSON.stringify(state.type));
}, [key, state]);
return [state, setState];
}
我的主题:
黑暗的:
export default {
type: 'dark',
colors: {
primary: '#222',
secondary: '#DF2935',
background: '#333',
text: '#fff',
},
};
光:
export default {
type: 'light',
colors: {
primary: '#3772FF',
secondary: '#DF2935',
background: '#f5f5f5',
text: '#333',
},
};
我不知道如何解决这个问题我基本上将我的主题保存在我的本地存储中以便能够保存我的偏好
解决方案
推荐阅读
- python - Pandas groupby + scikit 学习电源变压器
- java - 有没有办法在用 Java 创建文件时将文件发送到 AWS S3 存储桶?
- vue.js - 在 vuejs 中,拖动和调整图像大小时出现问题
- angular - 命名路由器出口在刷新后不呈现相同的组件
- mysql - SQL 按组排序
- php - 我如何通过它们的值在 PHP 中的选择元素中更改页面的位置?
- python - 在 Python 中将用户输入转换为嵌套列表
- r - 如何计算取决于2个条件
- python - 如何使用 Pandas 将变量分配给 Python 中的列
- html - 获取引导按钮和图像以在 div 上正确对齐