How do I override compiled classes?

风流意气都作罢 提交于 2019-12-11 18:26:35

问题


I cam trying to apply override styles to a compiled class name. my compiled code is like...

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

I want to be able to target that specific item like this

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

in vanilla CSS I could just do .MultiListItemText-root-262: { color: red; }

How can I do the equivalent in JSS?


回答1:


You cannot do it this way. The classname "MuiListItemText-root-262" is dynamic, and the id "262" is not reliable and may change.

Please look at the official documentation of Material UI for using JSS overrides : https://material-ui.com/customization/overrides/

There are several techniques available depending on the level of variation your want to achieve.

For a typical "one time" override, see the first sample code which uses the 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);


来源:https://stackoverflow.com/questions/51541248/how-do-i-override-compiled-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!