【发布时间】:2020-07-03 10:46:27
【问题描述】:
我在哪里可以找到基于材料 ui 类的组件而不是基于函数的组件,如果可以的话。 在哪里写
const classes = useClasses();
const theme = useTheme();
在我的类组件中?
【问题讨论】:
标签: reactjs material-ui jss
我在哪里可以找到基于材料 ui 类的组件而不是基于函数的组件,如果可以的话。 在哪里写
const classes = useClasses();
const theme = useTheme();
在我的类组件中?
【问题讨论】:
标签: reactjs material-ui jss
useTheme - 它是一个钩子,可以与功能组件一起使用 但是,您可以将 withTheme 用于类组件。
【讨论】:
您的用例有两个 HoC,即 withStyles 和 withTheme。
import React from "react"
import { withStyles } from "@material-ui/core/styles"
const styles = theme => ({
wrapper: {
display: "flex",
// ...
}
// ...
})
class ClassComponent extends React.Component {
// ...
render() {
const { classes } = this.props
// ...
return (
<div className={classes.wrapper}>
{/* ... */}
</div>
)
}
}
export const EnhancedComponent = withStyles(styles)(ClassComponent)
现在您可以渲染EnhancedComponent。
【讨论】: