It works
var Box = React.createClass({ getInitialState: function() { return { color: 'white' }; }, changeColor: function() { var newColor = this.state.color == 'white' ? 'black' : 'white'; this.setState({ color: newColor }); }, render: function() { return ( <div > <div className='box' style={{background:this.state.color}} onClick={this.changeColor}> In here already </div> </div> ); } }); ReactDOM.render(<Box />, document.getElementById('div1')); ReactDOM.render(<Box />, document.getElementById('div2')); ReactDOM.render(<Box />, document.getElementById('div3'));
and in your css remove the styles you have and put this
.box { width: 200px; height: 200px; border: 1px solid black; float: left; }
You need to style the actual div you call onClick
. Give the div the name of the class, and then tweak it. Also do not forget to delete this block where you show the App
in dom, the application is not defined
ReactDOM.render(<App />,document.getElementById('root'));
source share