I wanted to see my contained primary button's color was slightly changed whenever the theme was changed, but I couldn't.
I saw it worked if I changed the button from the contained button to the text button by removing 'variant="contained"', but I really want to use the contained button
Is there any way to make it work?
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(
// Because of <React.StrictMode>, <CssBaseline /> didn't work when theme was changed.
// <React.StrictMode>
// <App/>
// </React.StrictMode>,
<App />,
document.getElementById('root')
);
App.js
import React, { useState } from 'react';
import './App.css';
import { Button, Container } from '@material-ui/core';
import ChangeTheme from './components/ChangeTheme';
import { lightTheme, darkTheme } from './libs/Theme';
import { MuiThemeProvider, CssBaseline} from '@material-ui/core';
function App() {
const [theme, setTheme] = useState(darkTheme);
const handleChangeTheme = (setDark) => {
setDark === true ? setTheme(darkTheme) : setTheme(lightTheme);
}
return (
<MuiThemeProvider theme={theme}>
<CssBaseline />
<Button variant="contained" color="primary">Hi</Button>
</MuiThemeProvider>
);
}
export default App;
libs/Theme.js
import { createMuiTheme, responsiveFontSizes } from '@material-ui/core';
export const lightTheme = responsiveFontSizes(
createMuiTheme({
palette: {
type: 'light'
}
})
);
export const darkTheme = responsiveFontSizes(
createMuiTheme({
palette: {
type: 'dark'
}
})
);