【发布时间】:2020-10-10 14:00:41
【问题描述】:
我正在关注the official tutorial 为我的应用创建全局主题。
我的根组件应该提供全局主题:
const themeInstance = {
backgroundColor: 'cadetblue'
}
render (
<ThemeProvider theme={themeInstance}>
<ChildComponent />
</ThemeProvider>,
rootNode
)
但我似乎无法让我的子组件应用主题:
const useStyles = makeStyles(theme => {
childComp: {
backgroundColor: theme.backgroundColor
}
})
const ChildComponent: FC = () => {
const classes = useStyles()
return (
<div className={classes.childComp}>I'm a div</div>
)
}
<ChildComponent /> 被渲染为无样式。
这似乎是某种类型不匹配的问题,因为当我开始使用 theme 参数的类型时,浏览器已呈现预期的输出(<ChildComponent /> div 和 background-color: cadetblue)一瞬间比错误失败。
非常感谢任何帮助找出我哪里出错的方法。
可以在over here找到实时沙盒。
【问题讨论】:
标签: javascript reactjs typescript material-ui