【发布时间】:2021-08-01 20:56:12
【问题描述】:
我正在努力让 Material-ui 的主题与 Typescript 中的 react-router-dom 一起使用。在render() 中访问 classes.root 时,我得到 TypeError: Cannot read property 'root' of undefined,但我无法弄清楚我是如何错误地合并/传递道具的。谁能看到我做错了什么?非常感谢。
相关代码如下:
const styles = (theme: Theme) =>
createStyles({
root: {
maxWidth: 345,
},
});
interface HomeProps extends RouteComponentProps, WithStyles<typeof styles> {
classes: any;
}
export class Home extends React.Component<HomeProps, HomeState> {
constructor(props: HomeProps) {
super(props);
}
render() {
const { classes } = this.props;
// classes.root is undefined...
}
}
export default withRouter(withStyles(styles)(Home));
编辑添加:这个问题的答案没有解决这个问题:Typescript error when using withRouter(withStyles(styles)(ComponentName))
【问题讨论】:
标签: typescript material-ui react-router-dom