【问题标题】:Material-ui classes name changes on build, adds identifiers to each class name that are overriden by userMaterial-ui 类名称在构建时更改,为每个被用户覆盖的类名称添加标识符
【发布时间】:2019-10-23 14:50:44
【问题描述】:

问题是当使用 classes={{blah blah}} 时,它在本地工作正常,默认材料类名称也没有标识符。但是在其他一些机器上,css 坏了,在检查出了什么问题之后,我才知道 className 生成器或我不知道是什么,通过将计数器编号添加到我使用的 classNames 来更改我的覆盖。 所以现在它看起来像这样。

现在我不想再次重写 css 也不能,因为这是你重写 Mui 类的方式。生产构建很糟糕。

'& .MuiSelect-blah':{ 一些 CSS *woosh }

【问题讨论】:

  • 您使用的是什么版本的 Material-UI?
  • "@material-ui/core": "^4.2.1",
  • 默认情况下,版本 4+ 不会将数字后缀添加到 Mui CSS 类名称(v3 已添加)。 “其他机器”是否使用旧版本的 Material-UI?
  • 否,其他机器使用相同的代码。
  • 当你说“它在本地工作正常”时,你的意思是在开发模式下吗?还是生产版本在本地运行良好?

标签: reactjs material-ui


【解决方案1】:

由于标识符仅附加在类名的末尾,因此您可以使用您尝试定位的CSS selector to match all elements starting with the class name

div[class^='myclass'], div[class*=' myclass']{
    color: #F00;
}

假设您不想定位 MuiOutlinedInput-root 类名。

使用简单的辅助函数getSelector(),您可以像这样完成它:

import {makeStyles} from '@material-ui/core/styles';

const getSelector = (className) => `& div[class^='${className}'], div[class*=' ${className}']`;

const useStyles = makeStyles((theme) => ({
  input: {
    [getSelector('MuiOutlinedInput-root')]: { // this will generate the selector
      ...
    },
    '& input': {
      ...
    }
  },
}));


注意:The following syntax 仅适用于 ES6Babel
{
    [keyVariable]: value,
}  

【讨论】:

    【解决方案2】:

    在App主文件中试试:

     import React from 'react';
     import { StylesProvider, createGenerateClassName } from '@material-ui/core/styles';
    
     const generateClassName = createGenerateClassName({
       productionPrefix: 'some',
     });
    
     export default function App() {
        return (
         <StylesProvider generateClassName={generateClassName}>...</StylesProvider>
       );
     }
    

    【讨论】:

    • 设置后,我需要一个使用makeStyles() 的消费者吗?因为这已经部分解决了问题,但仍然有一些部分损坏了。
    猜你喜欢
    • 2019-12-06
    • 2021-11-13
    • 2015-01-22
    • 2014-09-07
    • 2014-12-13
    • 2021-01-17
    • 2012-09-16
    • 2021-03-08
    • 1970-01-01
    相关资源
    最近更新 更多