Javascript ReactJS:有一个带绑定的类名,另一个不带绑定的类名
如何将静态类名与动态类名结合起来?例如,在这方面:Javascript ReactJS:有一个带绑定的类名,另一个不带绑定的类名,javascript,html,css,reactjs,Javascript,Html,Css,Reactjs,如何将静态类名与动态类名结合起来?例如,在这方面: ,如果我这样做,this.state.color将不会呈现,因为它在引号中,但我希望两个类都有。如果您需要将state color作为类名添加到“cheese”中,那么您可以这样做 <button className={"cheese " + this.state.color}> 工作代码 var Hello = React.createClass({ getInitialState: function(){
,如果我这样做
,this.state.color将不会呈现,因为它在引号中,但我希望两个类都有。如果您需要将state color作为类名添加到“cheese”中,那么您可以这样做
<button className={"cheese " + this.state.color}>
工作代码
var Hello = React.createClass({
getInitialState: function(){
return {
color: 'blue'
};
},
handleClick: function(){
if (this.state.color === 'blue'){
this.setState({color: 'green'});
} else {
this.setState({color: 'blue'});
}
},
render: function() {
return <button className={"cheese " + this.state.color} onClick={this.handleClick}>My background is: {this.state.color}, Click me to change</button>;
}
});
React.render(<Hello name="World" />, document.getElementById('container'));
var Hello=React.createClass({
getInitialState:函数(){
返回{
颜色:“蓝色”
};
},
handleClick:function(){
如果(this.state.color==='blue'){
this.setState({color:'green'});
}否则{
this.setState({color:'blue'});
}
},
render:function(){
返回我的背景是:{this.state.color},单击我进行更改;
}
});
React.render(,document.getElementById('container');
如果需要将状态颜色作为类名添加到“cheese”中,则可以像
<button className={"cheese " + this.state.color}>
工作代码
var Hello = React.createClass({
getInitialState: function(){
return {
color: 'blue'
};
},
handleClick: function(){
if (this.state.color === 'blue'){
this.setState({color: 'green'});
} else {
this.setState({color: 'blue'});
}
},
render: function() {
return <button className={"cheese " + this.state.color} onClick={this.handleClick}>My background is: {this.state.color}, Click me to change</button>;
}
});
React.render(<Hello name="World" />, document.getElementById('container'));
var Hello=React.createClass({
getInitialState:函数(){
返回{
颜色:“蓝色”
};
},
handleClick:function(){
如果(this.state.color==='blue'){
this.setState({color:'green'});
}否则{
this.setState({color:'blue'});
}
},
render:function(){
返回我的背景是:{this.state.color},单击我进行更改;
}
});
React.render(,document.getElementById('container');
可能重复的可能重复的