简体   繁体   English

修改或覆盖Material-UI按钮CSS

[英]Modify or override material-ui button css

I have a material UI RaisedButton and I want to change the background color. 我有一个材质UI RaisedButton,我想更改背景颜色。

I have this: 我有这个:

const style = {
 backgroundColor: 'green'
};

export default class CreateLinksave extends React.Component {

 <RaisedButton
   label="Continue"
   style={style}/>

}

but when I refresh the page the material-ui style remains, so how can I override the style? 但是,当我刷新页面时,material-ui样式仍然保留,那么如何覆盖样式呢?

A second question is, how to avoid doing inline styling? 第二个问题是,如何避免进行内联样式化? I was thinking on create a js file with constants with the styles I need for my components, import it and access to my styles that way, is that a good approach? 我在考虑使用常量创建具有所需组件样式的js文件,然后导入它并以这种方式访问​​样式,这是一种好方法吗?

I'm new to React so some help would be nice... 我是React的新手,所以一些帮助会很好...

Regards. 问候。

You can override the style by passing in the attribute. 您可以通过传入属性来覆盖样式。 See the docs for which attributes are supported. 请参阅支持哪些属性的文档

creating a JS file (or multiple files) with your styling sound like a good idea, as long as the rules are simple. 只要规则简单,使用样式声音创建JS文件(或多个文件)是一个好主意。 if you find yourself merging & overriding styles it would be easier just keeping the style in the component file. 如果您发现自己合并和覆盖样式,将样式保留在组件文件中会更容易。

You'll end up with something like 你最终会得到类似

import {green} from './my-style/colors';
...
<RaisedButton label="change min" backgroundColor={green} />

If you are using Material UI you should use jss as seen in their documentation. 如果您使用的是Material UI,则应使用其文档中所见的jss。 And use the withStyles HOC. 并使用withStyles HOC。 Actual link https://material-ui.com/customization/css-in-js/ 实际链接https://material-ui.com/customization/css-in-js/

Here is an example: 这是一个例子:

import { withStyles } from '@material-ui/core/styles';

const style = {
   back: {
    background: green,
  },
};

class CreateLinksave extends React.Component {
{classes} = this.props;

 <RaisedButton
   label="Continue"
   style={classes.back}/>

}

export default withStyles(styles)(CreateLinksave );

I think you should check the documentation to have better and reusable components. 我认为您应该检查文档以拥有更好和可重用的组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM