reactjs - React Material-UI 框架中的类名约定

标签 reactjs material-ui

Material ui 库中是否使用了类名约定?我目前正在使用material-ui-next 。我注意到像“MuiFormControl-root-124”这样的类名到处都是,并且类名后面附加了数字。对于纸张元素“MuiPaper-root-18 MuiPaper-shadow2-22 MuiPaper-rounded-19”。我只是在这里看不到任何模式。

是否有我缺少的约定。如果这个框架像 Bootstraps 网格类一样有意义,那么理解这个框架会更容易。非常感谢所有帮助。谢谢。

最佳答案

在material-ui v1中,类名是在运行时不确定地生成的,因此没有您应该遵守的约定。描述的是here在文档中:

You may have noticed that the class names generated by our styling solution are non-deterministic, so you can't rely on them to stay the same.

但是,这并不重要,因为您一开始就不应该使用原始 CSS 类名。相反,您可以使用 withStyles为每个组件设置适当的样式:

import { withStyles } from 'material-ui/styles';

// Define the CSS styles you which to apply to your component
const styles = {
  root: {
    backgroundColor: 'red',
  },
};

class MyComponent extends React.Component {
  render () {
    // withStyles automatically provides the classes prop, which
    // will contain the generated CSS class name that you can match
    // to your component
    return <div className={this.props.classes.root} />;
  }
}

export default withStyles(styles)(MyComponent);

这些非确定性类名称具有技术优势,包括改进的性能:

Thanks to the non-deterministic nature of our class names, we can implement optimizations for development and production. They are easy to debug in development and as short as possible in production.

您应该注意,发生这种情况是因为material-ui 采用与 Bootstrap 这样的库完全不同的样式方法。虽然 Bootstrap 有一个 CSS 库,其中定义了应用于每个元素的类名,但 Material-ui 使用 CSS in JS注入(inject)造型。这使得 CSS 可以与每个组件的 JavaScript 定义一起定义和存储。

关于reactjs - React Material-UI 框架中的类名约定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48161450/

相关文章:

javascript - Material UI - DataGrid 自定义列表过滤器

reactjs - 通过 useRouter() 导航时,NextJS 13 中不显示暂挂回退

json - 为什么 mapbox 拒绝我的 geojson 为无效。

javascript - 在 Material UI 和 React 中的文本输入中添加图标

material-ui - material-ui@next 中的 TextField 样式

reactjs - Material UI AutoComplete 未在 React 应用程序中的远程数据源上呈现

reactjs - 无法让 Popover 显示在对话框中的正确位置

javascript - 我想仅在reactjs中更改单击按钮(onClick)的变体或背景。我怎样才能实现它?

reactjs - 使用 React Router V4 时如何防止组件立即卸载

reactjs - Material UI 与 React 16 的兼容性为 0.2 倍