【发布时间】:2020-10-24 18:58:06
【问题描述】:
我在我的 App.js 中创建了一个主题,它覆盖了主要颜色和次要颜色。我有 ThemeProvider 包装一个 Home 组件。覆盖的值不会显示在 Home 组件中。我做错了什么?
App.js
import React from 'react'
import { ThemeProvider, createMuiTheme } from '@material-ui/core/styles'
import purple from '@material-ui/core/colors/purple'
import green from '@material-ui/core/colors/green'
import Home from './components/Home'
const theme = createMuiTheme({
overrides: {
pallete: {
primary: {
main: purple[500]
},
secondary: {
main: green[500]
}
}
}
})
const App = () => {
return (
<ThemeProvider theme={theme}>
<Home />
</ThemeProvider>
)
}
export default App
Home.js
import React from 'react'
import { useTheme } from '@material-ui/core/styles'
import { Container, Grid, AppBar, Toolbar, CssBaseline } from '@material-ui/core'
const Home = () => {
const theme = useTheme()
return (
<Container max="lg">
<CssBaseline />
<Grid container>
<Grid item xs={12}>
<AppBar color="primary">
<Toolbar>
Hello World
</Toolbar>
</AppBar>
</Grid>
</Grid >
</Container >
)
}
export default Home
I would think that in my AppBar the color="primary" should show up with the overridden primary color. But it's not happening.
【问题讨论】:
-
我猜你需要覆盖你的 muitheme 中的 Appbar 道具。请参阅此链接 - material-ui.com/customization/globals/#css 此外,如果您需要在组件级别覆盖它,则需要通过使用 makeStyles 并为该规则添加指定的 CSS 属性来覆盖它。
-
@Mohit 我想,他走对了。如果他想在应用程序中覆盖整个
palette,他需要在尝试时使用ThemeProvider。更具体的覆盖需要覆盖更具体的东西。 :)
标签: reactjs material-ui