【发布时间】:2018-01-09 06:54:18
【问题描述】:
我是 material-ui 的新手,在我的第一次测试(material v0.18.7 和 react v15.6.1)之后,我发现它对每个组件都使用了内联样式。 我测试了 10 次放置相同的组件,并且没有基于类的样式(使用 CSS)它只是重复内联样式。 就像在一个真实的网页中一样,可能有很多组件具有相同的样式,有没有更好的方法来停止做这种多余的样式并添加一些 css 或类似的东西,这可以减少传递给用户的数据大小每个页面加载?
我的意思是任何可以帮助我减少可以以其他方式处理的内联样式数量的东西
这是我的示例代码
<div>
<Paper style={style} zDepth={1} ><FlatButton label="exit" /></Paper>
<Paper style={style} zDepth={1} ><FlatButton label="exit" /></Paper>
<Paper style={style} zDepth={1} ><FlatButton label="exit" /></Paper>
</div>
我用 react 渲染这个简单的组件,输出的 html 文件有以下 div 三次:
<div style="color: rgba(0, 0, 0, 0.87); background-color: rgb(255, 255, 255); transition: all 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms; box-sizing: border-box; font-family: Roboto, sans-serif; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); box-shadow: rgba(0, 0, 0, 0.12) 0px 1px 6px, rgba(0, 0, 0, 0.12) 0px 1px 4px; border-radius: 2px; height: 100px; width: 100px; margin: 20px; text-align: center; display: inline-block;">
<button tabindex="0" type="button" style="border: 10px; box-sizing: border-box; display: inline-block; font-family: Roboto, sans-serif; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); cursor: pointer; text-decoration: none; margin: 0px; padding: 0px; outline: none; font-size: inherit; font-weight: inherit; position: relative; z-index: 1; height: 36px; line-height: 36px; min-width: 88px; color: rgba(0, 0, 0, 0.87); transition: all 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms; border-radius: 2px; user-select: none; overflow: hidden; background-color: rgba(0, 0, 0, 0); text-align: center;">
<div>
<span style="position: relative; padding-left: 16px; padding-right: 16px; vertical-align: middle; letter-spacing: 0px; text-transform: uppercase; font-weight: 500; font-size: 14px;">exit</span>
</div>
</button>
</div>
确切的div 被添加到我的代码中三次,所有内联样式。
我正在寻求一种方法来减少这些内联样式的数量并使用一些 css 代码来更改它们。
谢谢
【问题讨论】:
-
嘿@rkarami,要使用的组件属性是
className。然后您可以使用样式表文档/块来定义样式。 -
@yomexzo 我知道如何在反应中添加类,我对反应中的那些内联样式有很大的问题,特别是在这个库中
-
我明白你的意思@rkarami。我有同样的看法。样式由 materialui 库添加。考虑到所有设备(尤其是移动设备)上的渲染,他们可能会使用它。
标签: javascript html css reactjs material-ui