How to override (overwrite) classes and styles in material-ui (React)

前端 未结 2 1014
忘了有多久
忘了有多久 2021-01-01 16:08

I\'m using version 1.2.1 of material-ui and I\'m trying to override the AppBar component to be transparent. The documentation outlines how to override styles here.

My

2条回答
  •  我在风中等你
    2021-01-01 16:33

    This answer makes @Nadun answer complete - he deserves the credit. To override material ui classes we need to understand these things:

    1. Add your styles in a const variable at the top

        const styles = {
          root: {
            backgroundColor: 'transparent !important',
            boxShadow: 'none',
            paddingTop: '25px',
            color: '#FFFFFF'
          }
        };
    

    2. We need to use higher order function with "withStyles" to override material ui classes

        export default withStyles(styles)(NavigationBar);
    

    3. Now these styles are available to us as props in the render function try doing this - console.log(this.props.classes) - you get a classes name correspoding to properties you include in styles object, like - {root: "Instructions-root-101"}.

    Add that with classes attribute

    render() {
       const { classes } = this.props;
       return ( 
           
            // Add other code here
           
       )
    }
    

提交回复
热议问题