例如,我有这个超级简单的“标题”组件,它基本上只是一个被美化过的标签。
请注意样式属性一遍又一遍地重复出现的情况?所以,我的问题是,这是一件坏事吗?这有关系吗?从我所知道的来看,这并不是什么坏事,因为你并没有预先从服务器下载这个html页面。因此,所有这些重复的样式并不会导致更大的文件大小。
标签。
import React from 'react';
class Heading extends React.Component {
render () {
const style = {
'color' : 'red'
};
return (
<h1 className='heading' style={style}>
{this.props.title}
</h1>
);
}
}
export default Heading;
这将在DOM中呈现为以下内容:
<h1 class="heading" style="color: red;">Heading</h1>
import React from 'react';
class Heading extends React.Component {
render () {
const style = {
'color' : 'red'
};
return (
<h1 className='heading' style={style}>
{this.props.title}
</h1>
);
}
}
export default Heading;
<h1 class="heading" style="color: red;">Heading</h1>
很好,看起来不错。如果页面上有一堆这样的组件,它会像这样:
<h1 class="heading" style="color: red;">Heading</h1>
<h1 class="heading" style="color: red;">Heading</h1>
<h1 class="heading" style="color: red;">Heading</h1>
<h1 class="heading" style="color: red;">Heading</h1>
请注意样式属性一遍又一遍地重复出现的情况?所以,我的问题是,这是一件坏事吗?这有关系吗?从我所知道的来看,这并不是什么坏事,因为你并没有预先从服务器下载这个html页面。因此,所有这些重复的样式并不会导致更大的文件大小。
如果这是真的……那为什么不使用内联样式呢?
免责声明:我明白stackoverflow对于这类问题有些“封闭”。如果您对我在这里提出这个问题感到深深地冒犯了(我认为自己是谁!?),请友善地指引我去哪个网站和社区 :)
color: red;
变成redclass
或其他名称不会使页面大小发生太大变化,除非您向该类添加其他样式。但是想象一下,如果有人查看您的源代码?那将很尴尬! - Gray Fox