使用 MUI 5.14.1 时,每当我尝试使用 localStorage 通过页面保留主题时,都会收到错误。如果我能得到任何关于如何解决这个问题的帮助,或者关于如何以不同的方式解决这个问题的建议,我将不胜感激。
import React, { useState } from "react";
import {
CssBaseline,
Button,
Typography,
} from "@mui/material";
import { ThemeProvider } from "@mui/material/styles";
import { Toaster, toast } from "react-hot-toast";
import { Link } from "react-router-dom"; // Import the Link component from react-router-dom
import styles from "../styles/app.module.css";
import MuteSwitch from "../components/MuteSwitch.js";
import StyledAvatar from "../components/StyledAvatar.js";
import Sidebar from "../components/Sidebar";
import {
toggleDarkMode,
handleThemeChange,
lightTheme,
} from "../utils/themeUtils";
const persistedTheme = JSON.parse(localStorage.getItem("theme")) || lightTheme;
const HomePage = () => {
const [currentTheme, setCurrentTheme] = useState(persistedTheme); // Define the state variable for the current theme
const [darkMode, setDarkMode] = useState(false); // Track dark mode state, false = light mode, true = dark mode
const [userInputColor, setUserInputColor] = useState("#1976d2"); // Default initial color
const [colorPickerColor, setColorPickerColor] = useState("#1976d2"); // Default initial color
const saveTheme = (theme) => {
localStorage.setItem("theme", JSON.stringify(currentTheme));
};
const handleColorChange = (event) => {
setColorPickerColor(event.target.value);
setUserInputColor(event.target.value);
};
const handleDarkModeToggle = () => {
setDarkMode((prevMode) => !prevMode); // Toggle the dark mode state
toggleDarkMode(darkMode, setCurrentTheme);
};
const createToast = (message) => {
let toastBackground = currentTheme.palette.primary.main;
let toastColor = currentTheme.palette.primary.contrastText;
toast.success(message, {
style: {
background: toastBackground,
color: toastColor,
},
});
};
const handleNewMessages = () => {
createToast("You have 3 new messages");
};
const onThemeChange = () => {
//possibly darken color picker color
const updatedTheme = handleThemeChange(userInputColor);
setCurrentTheme(updatedTheme);
saveTheme(updatedTheme);
};
return (
<>
<Toaster />
<ThemeProvider theme={currentTheme}>
<CssBaseline />
<div className={styles.heading}>
<Typography variant="h1" component="h1" gutterBottom>
Home Page
</Typography>
</div>
{/* content */}
<div className={styles.centeredContent}>
<Button variant="contained">Pretty Colors</Button>
</div>
{/* mute switch */}
<div className={styles.muteSwitch}>
<MuteSwitch />
</div>
{/* avatar */}
<Link to="/profile" style={{ textDecoration: "none", color: "inherit" }}>
<div className={styles.avatar}>
<StyledAvatar>TS</StyledAvatar>
</div>
</Link>
{/* drawer */}
<div>
<Sidebar handleThemeChange={onThemeChange} darkMode={darkMode} handleDarkModeToggle={handleDarkModeToggle} handleNewMessages={handleNewMessages} colorPickerColor={colorPickerColor} handleColorChange={handleColorChange}/>
</div>
</ThemeProvider>
</>
);
};
export default HomePage;
错误:
Unexpected Application Error!
theme.transitions.create is not a function
TypeError: theme.transitions.create is not a function
at http://localhost:3000/static/js/bundle.js:11135:35
我尝试使用 localStorage 来存储它,希望它能让我在新文件中检索主题,但每当我编辑
const [currentTheme, setCurrentTheme] = useState(lightTheme);
以使用持久主题时,它都会给我一个错误。