【问题标题】:How do I override compiled classes?如何覆盖已编译的类?
【发布时间】:2018-07-26 14:27:34
【问题描述】:

我试图将覆盖样式应用于已编译的类名。我编译的代码就像...

<div class="MuiListItemText-root-262" >

我希望能够像这样定位特定项目

const styles = () => { MultiListItemText-root-262: { color: red; } }

在原版 CSS 中,我可以做到 .MultiListItemText-root-262: { color: red; }

如何在 JSS 中做同样的事情?

【问题讨论】:

  • 您可以通过为您的 MuiListItemText 定义一个样式对象来做到这一点。例如 const muiListStyle = { color: 'red' },那么你可以使用它 inline
  • 或者你可以创建一个css文件并导入它

标签: reactjs material-ui jss


【解决方案1】:

你不能这样做。 类名“MuiListItemText-root-262”是动态的,id“262”不可靠,可能会改变。

关于使用 JSS 覆盖,请查看 Material UI 的官方文档:https://material-ui.com/customization/overrides/

根据您想要达到的变化程度,有几种可用的技术。

对于典型的“一次性”覆盖,请参阅第一个使用 withStyles HOC 的示例代码

import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import Button from '@material-ui/core/Button';

// We can inject some CSS into the DOM.
const styles = {
  button: {
    background: 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)',
    borderRadius: 3,
    border: 0,
    color: 'white',
    height: 48,
    padding: '0 30px',
    boxShadow: '0 3px 5px 2px rgba(255, 105, 135, .3)',
  },
};

function ClassNames(props) {
  return (
    <Button className={props.classes.button}>
      {props.children ? props.children : 'class names'}
    </Button>
  );
}

ClassNames.propTypes = {
  children: PropTypes.node,
  classes: PropTypes.object.isRequired,
};

export default withStyles(styles)(ClassNames);

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签