2

How do I have a static className with a className that is dynamic? For example, in this: https://jsfiddle.net/uwadhwnr/112/

<button className={this.state.color}>, if I do <button className="cheese {this.state.color}">, the this.state.color won't render because it's in quotations, but I want to have both classes.

1

1 Answer 1

5

If you need to add the state color as className to "cheese" then you can do it like

<button className={"cheese " + this.state.color}> 

Working code

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.