【发布时间】:2017-09-01 10:40:03
【问题描述】:
我想在 div 标签中使用 this.props 数据。我在数据中有我想要动态设置的颜色。是否可以在div标签中使用this.props?
var cont = {
height: "90px",
width: "20%",
background: "LightBlue",
display: "inline-block",
padding: "5px",
margin: "5px"
};
var Comment = React.createClass({
render: function () {
return (
<div style={cont}>
<span>
{this.props.author}
{this.props.col}
</span>
</div>
);
}
});
【问题讨论】:
-
<div style={{color: this.props.color}}></div> -
我添加了一个样式,如何将 style={{backgroundColor: this.props.col}} 再次添加到同一个 div?像这样:
style={Object.assign({}, cont, {backgroundColor: 'red', border:'10px'})}或使用style={{...cont, backgroundColor: 'red'}}@MayankShukla 谢谢,以上解决方案有效
标签: reactjs reactjs.net