2017-03-29 27 views
-1

如何讓一個靜態的className具有動態的className?例如,在此: https://jsfiddle.net/uwadhwnr/112/ReactJS:有一個具有綁定的className和另一個沒有的類名稱

<button className={this.state.color}>,如果我做<button className="cheese {this.state.color}">,則this.state.color將不會呈現,因爲它是在報價單,但我想有兩個類。

+0

[串聯變量的可能的複製和React中的字符串](http://stackoverflow.com/questions/39523040/concatenating-variables-and-strings-in-react) – Li357

回答

1

如果需要國家的顏色添加爲類名「奶酪」,那麼你可以不喜歡它

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

JSFIDDLE

相關問題