Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ReactJS:有一个带绑定的类名,另一个不带绑定的类名_Javascript_Html_Css_Reactjs - Fatal编程技术网

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');

可能重复的可能重复的