I just can't figure it out. I want to change background with Switch, but it works only the first time, doesn't work on subsequent clicks. Here is code sandbox:
https://codesandbox.io/s/strange-shaw-2tfk5
Could someone enlighten me what is going on? I'm using Material UI with React
minimal reproducible example: this is my App component
import React from "react";
import { useState } from "react";
import { ThemeProvider, createTheme } from "@material-ui/core/styles";
import { Container, Switch, CssBaseline } from "@material-ui/core";
const darkTheme = createTheme({
palette: {
type: "dark",
background: {
default: "hsl(230, 17%, 14%)"
}
}
});
const lightTheme = createTheme({
palette: {
type: "light",
background: {
default: "hsl(0, 0%, 100%)"
}
}
});
const App = () => {
const [mode, setMode] = useState("light");
const selectedTheme = mode === "dark" ? darkTheme : lightTheme;
return (
<ThemeProvider theme={selectedTheme}>
<CssBaseline />
<Container maxWidth="lg">
<h1>Hello</h1>
<Switch onChange={() => setMode(mode === "light" ? "dark" : "light")} />
</Container>
</ThemeProvider>
);
};
export default App;