简体   繁体   English

在reactjs中单击MUI按钮时如何更改/自定义样式?

[英]How do i change/customize style when MUI button is clicked in reactjs?

I'm trying to change the button appearance when i clicked it, but i think it doesn't work in reactjs like how you change how button will act just like in normal CSS, I want to change the background color of the button when i clicked it, also disable or enable the ripple effect.当我单击它时,我正在尝试更改按钮的外观,但我认为它在 reactjs 中不起作用,就像你如何更改按钮的行为方式一样,就像在普通 CSS 中一样,我想在我更改按钮的背景颜色时单击它,也禁用或启用波纹效果。

<Box component="center">
        <Button sx={{
          '&:hover': {
        color: 'red',
        backgroundColor: 'transparent',
        ':active':{
          backgroundColor: 'transparent'
        },
        
        },
        }} >
          <Typography
            sx={{
              mt: 0.5,
              textDecorationLine: "underline",
              textTransform: 'none',
              color: 'black'
            }}
          >
            Already have an account? Log in!
          </Typography>
          </Button>
          </Box>

You have syntactic errors in your style object and this is the reason why customization doesn't work.您的样式对象中有语法错误,这就是自定义不起作用的原因。 You have to pass styles for hover and active states as 2 separate objects.您必须将hoveractive状态的样式作为 2 个单独的对象传递。

As for disabling ripple effect, you can pass disableRipple to your Button component to turn off ripple effect.至于禁用波纹效果,您可以将disableRipple传递给您的Button组件以关闭波纹效果。

const styleObj = {
  "&:hover": {
    backgroundColor: "red"
  },
  "&:active": {
    backgroundColor: "blue"
  }
};
...
      <Button disableRipple sx={styleObj} variant="text">
        Text
      </Button>

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

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