您不能
makeStyles与类组件一起使用。
makeStyles返回一个只能在功能组件中使用的自定义钩子。对于类组件,可以使用withStyles来利用所有相同的功能。
withStyles包装您的组件并注入
classes道具。
下面是一个基于您的问题代码的工作示例:
import React from "react";import { withStyles } from "@material-ui/core/styles";class SearchBar extends React.Component { render() { return <div>My Search Bar</div>; }}const styles = theme => ({ root: { backgroundColor: theme.palette.primary.main, color: theme.palette.primary.contrastText }});class Nav extends React.Component { render() { return ( <div className={this.props.classes.root}> <SearchBar /> </div> ); }}export default withStyles(styles)(Nav);


