最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - react + Material ui makeStyles with '!important' - Stack Overflow

programmeradmin0浏览0评论

I am using makeStyles for adding styles to button, but in app there is included some other third party css file that overrides my styles without '!important'. I tried this approach, but it's not working, property is not generated in browser:

const useStyles = makeStyles({
  button: {
    backgroundColor: props => props.actionBackgroundColor,
    borderColor: props => props.actionBackgroundColor,
    color: props => {
      if (props.actionTextColor) {
        return `${props.actionTextColor} !important`;
      }
      return null;
    },
    '&:hover': {
      backgroundColor: props => props.actionHoverColor,
      borderColor: props => props.actionHoverColor,
      color: props => {
        if (props.actionHoverTextColor) {
          return `${props.actionHoverTextColor ? props.actionHoverTextColor : props.actionTextColor ? props.actionTextColor : null} !important`;
        }
        return null;
      }
    }
  }
});

I am using makeStyles for adding styles to button, but in app there is included some other third party css file that overrides my styles without '!important'. I tried this approach, but it's not working, property is not generated in browser:

const useStyles = makeStyles({
  button: {
    backgroundColor: props => props.actionBackgroundColor,
    borderColor: props => props.actionBackgroundColor,
    color: props => {
      if (props.actionTextColor) {
        return `${props.actionTextColor} !important`;
      }
      return null;
    },
    '&:hover': {
      backgroundColor: props => props.actionHoverColor,
      borderColor: props => props.actionHoverColor,
      color: props => {
        if (props.actionHoverTextColor) {
          return `${props.actionHoverTextColor ? props.actionHoverTextColor : props.actionTextColor ? props.actionTextColor : null} !important`;
        }
        return null;
      }
    }
  }
});

is there any workaround?

Share Improve this question asked Dec 24, 2019 at 8:43 vakho papidzevakho papidze 4653 silver badges13 bronze badges 2
  • Are this styles that you want to override ing from the material-ui ponents that you are trying to use? – yuri Commented Dec 24, 2019 at 8:46
  • no, As I said it's some custom third party library, just css file. – vakho papidze Commented Dec 24, 2019 at 8:54
Add a ment  | 

2 Answers 2

Reset to default 2

Can u please check this sandbox. There is a CustomButton ponent where I implemented your solution and it's working fine.

Code Sandbox

Seems I was using old version of material ui;

"@material-ui/core": "^3.9.2", "@material-ui/styles": "^4.4.1",

That's issue, seems '!important' does not work with that version. So I need to upgrade it.

发布评论

评论列表(0)

  1. 暂无评论