【问题标题】:Classname doesn't override the root style of AppBarClassname 不会覆盖 AppBar 的根样式
【发布时间】:2019-09-01 03:47:03
【问题描述】:

我正在尝试使用 className 更改 AppBar 组件的样式,但始终遵循 .MuiAppBar-root

const useBoardStyles = makeStyles((theme: Theme) => ({
  appBar: {
    backgroundColor: 'red',
    color: 'black',
    zIndex: theme.zIndex.drawer + 1
  },

const classes = useBoardStyles();

...
<AppBar position='fixed' className={classes.appBar} >

这里显示.makeStyles-appBar-139.MuiAppBar-colorPrimary 覆盖,background-colorcolor.MuiAppBar-root 覆盖,z-index

我也试过用classes

<AppBar position='fixed' classes={{ root: styles.appBar }} >

还是一样。

编辑:我正在使用打字稿。

【问题讨论】:

    标签: typescript material-ui


    【解决方案1】:

    与默认的“MuiAppBar-colorPrimary”相比,您的 CSS("makeStyles-appBar") 似乎是最后应用的,

    针对您的情况的几种解决方案,

    1。使用样式属性

    <AppBar position="fixed"  style={{ color: 'black', z-index: 1201, background-color: 'red'}}>
    

    2。在你的 CSS 上使用 !important - 不可取

    appBar: {
        backgroundColor: 'red !important',
        color: 'black !important',
        zIndex: (theme.zIndex.drawer + 1) + ' !important'
    } 
    

    参考 - Transparent AppBar in material-ui (React)

    https://material-ui.com/api/app-bar/

    TypeScript - 带有装饰器的参考如下,

    import * as React from 'react';
    import { withStyles, WithStyles } from 'material-ui/styles';
    import { StyledComponent } from 'material-ui';
    
    type C = 'root' | 'foo';
    
    interface P { options?: string[]; }
    
    interface S { open: boolean; }
    
    @withStyles((theme) => ({
      root: {},
      foo: {},
    }))
    class Component extends React.Component<P & WithStyles<C>, S> {
      render() {
        return (
          <div className={this.props.classes.root} />
        );
      }
    }
    
    export default Component as StyledComponent<P, C>; // type assertion
    

    没有装饰器

    const SelectedMenu = withStyles(theme => ({
      root: {
        maxWidth: 360,
        width: '100%',
        backgroundColor: theme.palette.background.paper,
      },
    }))<P>(class extends React.Component<P & WithStyles<C>, S> {
    

    来源 - https://github.com/mui-org/material-ui/issues/8598

    【讨论】:

    • 我在使用第一个选项时遇到问题,因为我使用的是打字稿。对不起,如果我没有在上面提到它。该错误说明了意外令牌,但我确定这与语法无关。
    • 您是否可以分享您看到的这个确切错误 - “错误说一些关于意外令牌的内容”
    • 您可以阅读此内容并应用相同内容并尝试吗? - github.com/mui-org/material-ui/issues/8598
    • 它现在正在工作。但是我想知道装饰器是否仅用于类组件?正如此处为typescript 定义的那样。而且withStyles是否只是这个问题的可能解决方案?
    • 这是我记得的最好的解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    • 1970-01-01
    • 2021-11-21
    • 2021-08-02
    • 2019-02-20
    • 2015-04-07
    • 1970-01-01
    相关资源
    最近更新 更多