如何覆盖material-ui中的类和样式(React) [英] How to overwrite classes and styles in material-ui (React)

查看:453
本文介绍了如何覆盖material-ui中的类和样式(React)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 material-ui 的1.2.1版本,而我正在尝试覆盖 AppBar 组件要透明。该文档概述了如何覆盖样式此处

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

我的代码:

import React, { Component } from 'react';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import logo from '../Assets/logo.svg';

class NavigationBar extends Component {
  render() {
    const styles = {
      root: {
        backgroundColor: 'transparent',
        boxShadow: 'none',
      },
    };

    return (
      <AppBar position={this.props.position} classes={{ root: styles.root }}>
        <Toolbar>
          <img src={logo} style={{ height: '28px' }} />
        </Toolbar>
      </AppBar>
    );
  }
}

export default NavigationBar;

但这不会产生任何结果。我试图覆盖错误吗?不知道我在哪里错了...

But this yields no results. Am I trying to overwrite wrong? Not sure where I'm going wrong here...

推荐答案

这个答案让@Nadun回答完整 - 他值得信任。
要覆盖材料ui类,我们需要了解这些内容

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

1。在顶部的const变量中添加样式

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

2。 我们需要使用带有withStyles的高阶函数来覆盖材料ui类

    export default withStyles(styles)(NavigationBar);

3。现在这些样式可用作渲染中的道具函数
尝试这样做 - console.log(this.props.classes) - 你得到一个类名称对应你在styles对象中包含的属性。
喜欢 - {root:Instructions-root-101}

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"}.

在课堂上添加属性

render() {
   const { classes } = this.props;
   return ( 
       <AppBar classes={{ root: classes.root }}>
        // Add other code here
       </AppBar>
   )
}

这篇关于如何覆盖material-ui中的类和样式(React)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆