How do you add a CSS class to an existing REACT element on click?
I have a JSFiddle created: https://jsfiddle.net/5r25psub/
In the fiddle, the code only works if I have the statement: this.setState({color:blue});
I want something like this.setState({className: 'green'});
What am I doing wrong?
Code:
<html>
<script>
var Hello = React.createClass({
getInitialState: function(){
return {
color: 'blue'
};
},
handleClick: function(){
if (this.state.color === 'blue'){
this.setState({className = " green"});
} else {
this.setState({color: 'blue'});
}
},
render: function() {
return <button className={this.state.className} onClick={this.handleClick}>My background is: {this.state.color}, Click me to change</button>;
}
});
React.render(<Hello name="World" />, document.getElementById('container'));
</script>
<body>
<script src="https://facebook.github.io/react/js/jsfiddle-integration.js"></script>
<style>
.green {
background-color: lightgreen;
}
.blue {
background-color: lightblue;
}
</style>
<div id="container">
<!-- This element's contents will be replaced with your component. -->
</div>
</body>
</html>
handleClickyou wroteclassName = " green"but it should beclassName: "green"