我对反应还很陌生,我正在使用 Material-UI 实现一些新功能。我正在尝试在当前主题本地存储在浏览器中时在浅色和深色主题之间切换。我希望在页面刷新时本地存储当前主题。默认情况下,我的应用会加载到浅色主题中。在切换开关之前,我确认轻主题在本地存储中。当我切换到深色主题时,我的应用程序会更改为对应的应用程序,并且本地存储中的值会按预期更改。但是当我尝试将开关切换回浅色主题时,主题并没有改变,只是我的本地存储值。我不知道为什么。
如果有人可以调整此代码以解决此问题或提出新想法,我将不胜感激。
CodeSandbox 中的演示
CustomThemeContext.js
import React, { useState, createContext } from 'react';
import { ThemeProvider } from '@material-ui/core/styles';
import getTheme from '../components/MyThemes';
import Paper from '@material-ui/core/Paper';
export const CustomThemeContext = createContext(
{
currentTheme: 'normalTheme',
setTheme: null,
},
);
const CustomThemeProvider = (props) => {
const { children } = props
const currentTheme = localStorage.getItem('appTheme') || 'normalTheme'
const [themeName, _setThemeName] = useState(currentTheme)
const theme = getTheme(themeName)
const setThemeName = (name) => {
localStorage.setItem('appTheme', name)
_setThemeName(name)
}
const contextValue = {
currentTheme: themeName,
setTheme: setThemeName,
}
const paperStyle = {
height: "100vh",
}
return (
<div>
<CustomThemeContext.Provider value={contextValue}>
<ThemeProvider theme={theme}>
<Paper style={paperStyle}>
{children}
</Paper>
</ThemeProvider>
</CustomThemeContext.Provider>
</div>
)
}
export default CustomThemeProvider;
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import CustomThemeProvider from './contexts/CustomThemeProvider';
ReactDOM.render(
<React.StrictMode>
<CustomThemeProvider>
<App />
</CustomThemeProvider>
</React.StrictMode>,
document.getElementById('root')
);
serviceWorker.unregister();
应用程序.js
import React, { useContext } from 'react';
import { makeStyles } from '@material-ui/core/styles';
import { CustomThemeContext } from './contexts/CustomThemeProvider';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import Tooltip from '@material-ui/core/Tooltip';
import Typography from '@material-ui/core/Typography';
import BurgerButton from './components/BurgerButton';
import StarButton from './components/StarButton';
import Switch from '@material-ui/core/Switch';
import Button from '@material-ui/core/Button';
import AddCircleIcon from '@material-ui/icons/AddCircle';
import FormControlLabel from '@material-ui/core/FormControlLabel';
const useStyles = makeStyles((theme) => ({
root: {
flexGrow: 1,
width: 'auto',
},
title: {
flexGrow: 1,
},
content: {
flexGrow: 1,
padding: theme.spacing(3),
}
}));
export default function App() {
const classes = useStyles();
const { currentTheme, setTheme } = useContext(CustomThemeContext);
const isDark = Boolean(currentTheme === 'darkTheme')
const handleThemeChange = (event) => {
const { checked } = event.target
if (checked) {
setTheme('darkTheme')
} else {
setTheme('normalTheme')
}
}
/*
switch (checked) {
case 'darkTheme':
return setTheme('darkTheme');
case 'normalTheme':
return setTheme('normalTheme');
default:
return null;
}
*/
return (
<div className="App">
<div className={classes.root}>
<AppBar position="static" title="Memo App">
<Toolbar>
<BurgerButton />
<Typography variant="h6" className={classes.title}>
Memo App
</Typography>
<Tooltip title="Toggle light theme/dark theme">
<FormControlLabel
control={<Switch checked={isDark} onChange={handleThemeChange} />}
label="Theme"
/>
</Tooltip>
<StarButton />
</Toolbar>
</AppBar>
<Button>
<AddCircleIcon />
</Button>
</div>
</div>
);
}
我的主题.js
import normalTheme from './themes/normalTheme';
import darkTheme from './themes/darkTheme';
const themes = {
normalTheme,
darkTheme,
}
export default function getTheme(theme) {
return themes[theme]
}