How to use useStyle to style Class Component in Material Ui

前端 未结 5 1885
我寻月下人不归
我寻月下人不归 2020-12-14 14:39

I want to use useStyle to style the Class Component . But this can be easily done hooks. but i want to use Component instead. But I cant figure out how to do this.

5条回答
  •  自闭症患者
    2020-12-14 15:00

    You can do it like this:

    import { withStyles } from "@material-ui/core/styles";
    
    const styles = theme => ({
      root: {
        backgroundColor: "red"
      }
    });
    
    class ClassComponent extends Component {
      state = {
        searchNodes: ""
      };
    
      render() {
        const { classes } = this.props;
        return (
          
    Hello!
    ); } } export default withStyles(styles, { withTheme: true })(ClassComponent);

    Just ignore the withTheme: true if you aren't using a theme.


    To get this working in TypeScript, a few changes are needed:

    import { createStyles, withStyles, WithStyles } from "@material-ui/core/styles";
    
    const styles = theme => createStyles({
      root: {
        backgroundColor: "red"
      }
    });
    
    interface Props extends WithStyles{ }
    
    class ClassComponent extends Component {
    
    // the rest of the code stays the same
    

提交回复
热议问题