I created three files shown below.
The color of Paper changes according to the value of palette.type defined in theme.js.
Page with palette.type: 'dark'
Page with palette.type: 'light'
However, I don't understand how the variable theme works. I added two console.log(theme) to index.js. Both shows palette.type: 'light' regardless of the value in theme.js.
Result of console.log
How can I pass theme to index.js (without importing theme)?
components/Layout.js - Shared component to apply the same layout to all pages
import React from 'react';
import { MuiThemeProvider } from '@material-ui/core/styles';
import { theme } from '../utils/theme';
class Layout extends React.Component {
render() {
return(
<MuiThemeProvider theme={theme}>
{this.props.children}
</MuiThemeProvider>
)
}
}
export default Layout;
pages/index.js - One specific page
import Layout from '../components/Layout';
import { makeStyles, createStyles, useTheme, Paper } from '@material-ui/core';
const useStyles = makeStyles(theme => {
console.log('inside makeStyles');
console.log(theme);
return createStyles({
container: {
margin: '5px 5px 50px 5px'
}
})
});
export default function Index(props) {
const classes = useStyles(props);
const theme = useTheme();
console.log('inside Index')
console.log(theme);
return(
<Layout>
<h2>About</h2>
<Paper className={classes.container}>
<p>Sample test</p>
</Paper>
<Paper className={classes.container}>
<p>Sample test2</p>
</Paper>
</Layout>
)
};
utils/theme.js - Separate file to write theme
import { createMuiTheme } from '@material-ui/core/styles';
export const theme = createMuiTheme({
palette: {
type: 'dark'
}
});



Paper, however it also should change background color of the page if used correctly.