I'm making a very simple application where you can click on square divs to change their color from white to black. However, I'm having trouble. I'd like to use the onClick function to allow a user to click on a square to change its color, but it doesn't seem to be working. I've tried using spans and empty p tags, but that doesn't work either.
Here is the code in question:
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
style = {{background: this.state.color}}
onClick = {this.changeColor}
>
</div>
</div>
);
}
});
Here's a link to my small project on CodePen. http://codepen.io/anfperez/pen/RorKge
const
orlet
``` lang-jsx const Box = () =>{ const [color, setColor] = useState('white') const changeColor = ()=> setColor(previousColor=>previousColor==='white' ? 'black' : 'white') return // the JSX ``` – Rossiter