reactjs - 我的 Material UI Button 组件的自定义 makeStyles 被 ..css-zln006-MuiButtonBase-root-MuiButton-root 覆盖

标签 reactjs react-hooks material-ui

我的 MaterialUI Button 组件的自定义 makeStyles 被 .css-zln006-MuiButtonBase-root-MuiButton-root 覆盖。

我有一个 Material UI Button 组件:

import { Button } from "@mui/material";
import useStyles from "./HeaderStyles.js";

const Header = () => {
  const classes = useStyles();


  return (
    <React.Fragment>
      <AppBar position="fixed">
        <Toolbar disableGutters>
          <Button
            className={classes.button}
            variant="contained"
            color="secondary"
          >
            Click me
          </Button>
        </Toolbar>
      </AppBar>
    </React.Fragment>
  );
};

export default Header;

我的 HeaderStyles.js 文件:

import { makeStyles } from "@mui/styles";

const useStyles = makeStyles((theme) => ({
  button: {
    fontFamily: "Pacifico",
    textTransform: "none",
    fontSize: "1rem",
    borderRadius: "50px",
    marginLeft: "50px",
    marginRight: "25px",
    height: "45px",
  },
}));

export default useStyles;

我的样式被 .css-zln006-MuiButtonBase-root-MuiButton-root 类覆盖。 这个类正在取代我的按钮样式。

.css-zln006-MuiButtonBase-root-MuiButton-root {
display: '-webkit-inline-box',
display: '-webkit-inline-flex',
display: '-ms-inline-flexbox',
display: 'inline-flex',
WebkitAlignItems: 'center',
WebkitBoxAlign: 'center',
MsFlexAlign: 'center',
alignItems: 'center',
WebkitBoxPack: 'center',
MsFlexPack: 'center',
WebkitJustifyContent: 'center',
justifyContent: 'center',
position: 'relative',
boxSizing: 'border-box',
WebkitTapHighlightColor: 'transparent',
backgroundColor: 'transparent',
outline: '0',
border: '0',
margin: '0',
borderRadius: '0',
padding: '0',
cursor: 'pointer',
WebkitUserSelect: 'none',
MozUserSelect: 'none',
MsUserSelect: 'none',
userSelect: 'none',
verticalAlign: 'middle',
MozAppearance: 'none',
WebkitAppearance: 'none',
WebkitTextDecoration: 'none',
textDecoration: 'none',
color: 'inherit',
fontFamily: '"Roboto","Helvetica","Arial",sans-serif',
fontWeight: '500',
fontSize: '0.875rem',
lineHeight: '1.75',
letterSpacing: '0.02857em',
textTransform: 'uppercase',
minWidth: '64px',
padding: '6px 16px',
borderRadius: '4px',
WebkitTransition: 'background-color 250ms cubic-bezier(0.4, 0, 0.2, 1) 0ms,box-shadow 250ms cubic-bezier(0.4, 0, 0.2, 1) 0ms,border-color 250ms cubic-bezier(0.4, 0, 0.2, 1) 0ms,color 250ms cubic-bezier(0.4, 0, 0.2, 1) 0ms',
transition: 'background-color 250ms cubic-bezier(0.4, 0, 0.2, 1) 0ms,box-shadow 250ms cubic-bezier(0.4, 0, 0.2, 1) 0ms,border-color 250ms cubic-bezier(0.4, 0, 0.2, 1) 0ms,color 250ms cubic-bezier(0.4, 0, 0.2, 1) 0ms',
color: 'rgba(0, 0, 0, 0.87)',
backgroundColor: '#FFBA60',
boxShadow: '0px 3px 1px -2px rgba(0,0,0,0.2),0px 2px 2px 0px rgba(0,0,0,0.14),0px 1px 5px 0px rgba(0,0,0,0.12)'
}

我的按钮样式

.makeStyles-button-59 {{
height: '45px',
fontSize: '1rem',
fontFamily: 'Pacifico',
marginLeft: '50px',
marginRight: '25px',
borderRadius: '50px',
textTransform: 'none'
}

如何解决这个问题?

最佳答案

@mui/styles is the legacy styling solution for MUI. It is deprecated in v5. It depends on JSS as a styling solution, which is not used in the @mui/material anymore. If you don't want to have both emotion & JSS in your bundle, please refer to the @mui/system documentation which is the recommended alternative.

使用 sx 属性代替

const buttonStyles = {
  borderRadius: 50,
  fontFamily: "Pacifico",
  fontSize: "1rem",
  textTransform: "none",
};

<Button sx={buttonStyles} variant="contained" color="secondary">
  Click me
</Button>

Edit cocky-turing-uvycd

关于reactjs - 我的 Material UI Button 组件的自定义 makeStyles 被 ..css-zln006-MuiButtonBase-root-MuiButton-root 覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70875597/

相关文章:

javascript - 收到错误 "Cannot update a component while rendering a different component"。实现我正在尝试的正确方法是什么?

javascript - Material UI 如何设置网格元素垂直跨3行?

reactjs - 如何使用 css 替换 react-bootstrap 中的面包屑分隔符?

javascript - 将 React 组件从函数重构为 ES6 类

Reactjs useEffect 获取数据后的清理功能

reactjs - React Hooks-如何从查询字符串获取参数值

javascript - 如何覆盖 Material UI 工具提示内联样式?

reactjs - React Hook Form onChange 选择 Materialui

javascript - 更新组件时 react 输入元素不更新

javascript - svg 文件中的 require.context 给出了不同的结果