reactjs - 如何在 React 和 MUI 中使用 useStyles() 覆盖标签

标签 reactjs material-ui

我试图用 makeStyles() 覆盖 React-Router 的 Link 标签以删除链接装饰。我是否错误地使用了 makeStyles?默认文本下划线仍然显示。

const useStyles = makeStyles((theme) => ({
  root: {
    display: "flex",
  },
  Link: {
    textDecoration: "none",
  },
}));

最佳答案

makeStyles函数不会覆盖任何与 Mui 相关的样式定义。 makeStyles 的用法是为您提供一种简单的方法来创建新类然后使用它们。

例如:

const useStyles = makeStyles(theme => ({
    myLayout: {
        width: "auto",
        background: "red"
    }
}));
const classes = useStyles();
...
<div className={classes.myLayout}>

如果你想覆盖 Mui 组件的整个定义,你需要知道它是哪个组件,它的内部 Mui 名称是什么,然后你可以使用 createMuiTheme为此:

const muiTheme = createMuiTheme({
    overrides: {
        MuiLink: {
            root: {
                textDecoration: "none"
            }
        },
    }
});
...
<MuiThemeProvider theme={muiTheme}>
    <Link />
</MuiThemeProvider>

如果您只想更改一个特定链接(而不是覆盖网站中所有链接的样式定义),您可以使用 makeStyles然后使用 <Link /> 中的特定类组件:

const useStyles = makeStyles(theme => ({
    noDecoration: {
        textDecoration: "none"
   }
}));
const classes = useStyles();
...
<Link className={classes.noDecoration}>

Note - if you are using the <Link /> component from react-router-dom - this is not a MUI component, so it will not have any MUI related classnames. You can check the example here on how to design the router's link based on the MUI components.

关于reactjs - 如何在 React 和 MUI 中使用 useStyles() 覆盖标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61582648/

相关文章:

javascript - 在 React material-ui 上显示对话框

javascript - React - Material UI Typography 如何将长字符串分成多行

ruby-on-rails - Rails API ActiveStorage : Get Public URL to display image from AWS S3 Bucket?

javascript - 具有动态代码分割的自动 vendor block

javascript - React useReducer 调度在使用 fetch 请求后不会重新渲染组件

reactjs - 使用修改后的 TextField 无法使用 Material UI 自动完成功能

javascript - Material ui - 无法设置从选择验证器打开的弹出菜单的高度

javascript - setState 更新挂载状态

javascript - 当单元格内的文本更改为另一个值时,如何在单元格内添加样式元素

css - 如何隐藏 Material ui 表格单元格中的溢出内容而不是换行