问题
I can't seem to get my theme file to have any effect on my components in a Gatsby app. I'm using gatsby-plugin-material-ui
relevant part of gatsby-config.js
`gatsby-plugin-netlify-cms`,
{
resolve: `gatsby-plugin-material-ui`,
options: {
pathToTheme: 'src/theme.js',
},
theme.js
import { createMuiTheme } from '@material-ui/core/styles';
import indigo from '@material-ui/core/colors/indigo';
import pink from '@material-ui/core/colors/pink';
import red from '@material-ui/core/colors/red';
const theme = createMuiTheme({
palette: {
primary: indigo,
secondary: pink,
error: red,
},
spacing: {
100
}
})
export default theme;
within the component:
imports...
import { withStyles } from '@material-ui/core/styles';
const styles = theme => ({
heroText: {
color:'white',
textAlign: 'center',
lineHeight:7
},
mainBlogCopy: {
marginTop: theme.spacing.unit,
backgroundColor:theme.palette.primary
},
});
export default withStyles(styles)(AboutPage);
package.json
"dependencies": {
"@material-ui/core": "^3.9.3",
"gatsby": "^2.3.24",
"gatsby-image": "^2.0.34",
"gatsby-plugin-manifest": "^2.0.24",
"gatsby-plugin-material-ui": "^1.2.4",
more...
How does one use a theme in another file with this plugin?
回答1:
Create a file maybe named mui-root-wrapper.js
at your root directory
// mui-root-wrapper.js
import React from 'react'
import { ThemeProvider } from '@material-ui/core/styles'
import theme from './src/theme.js'
export default ({ element }) => (
<ThemeProvider theme={theme}>
{element}
</ThemeProvider>
)
Then import this component to both gatsby-browser.js
and gatsby-ssr.js
:
// gatsby-browser.js, gatsby-ssr.js
import muiRootWrapper from "./mui-root-wrapper"
export const wrapRootElement = muiRootWrapper
来源:https://stackoverflow.com/questions/55821752/where-to-put-theme-file-when-using-the-gatsby-plugin-material-ui-with-gatsby