您可以将多个 css 样式应用于一个 React 组件。
见下面一堆代码:
var MultipleCss = React.createClass({
getInitialState: function(){
return {
score: 20
}
},
render: function() {
const starrate = {
star:{
clear: 'none',
float: 'left',
margin: '0px 4px 0px 0px',
display: 'block',
width: '15px',
height: '15px',
minWidth: '15px',
padding: '0px',
cursor: 'pointer',
background: 'url(' + 'sprite.png' + ') no-repeat'
},
position:{
backgroundPosition: '-257px -147px'
},
active: {
backgroundPosition: '-235px -147px'
}
};
return (
< div>
<label style={Object.assign({}, starrate.star, starrate.position)}> </label>
</div>
);
}
});
假设我想根据条件在标签上添加 css(active)。所以我们可以这样做:
<label style={Object.assign({}, starrate.star, starrate.position, this.state.score >=20 && starrate.active)}> </label>
所以当 state.score >=20 时,活动类将应用于标签。