New to ReactJS.
I'm attempting to build a little component that moves some components around a container. The idea is that the user clicks on a button and the divs position changes.
I've tried to use Object.keys
and Object.entries
neither of them worked. I tried to create an array out of this.state
so that I could just do array.map()
but it did not work.
constructor(props) {
super(props);
this.handleShuffle = this.handleShuffle.bind(this);
this.state = {
redLeft: 0,
redTop: 0,
blueLeft: 0,
blueTop: 70
}
}
getRandomNumber (min, max) {
return min + (Math.floor(Math.random() * (max-min)))
}
handleShuffle() {
const min = 0;
const max = 230;
this.setState({
redLeft: this.getRandomNumber(min, max),
redTop: this.getRandomNumber(min, max),
blueLeft: this.getRandomNumber(min, max),
blueTop: this.getRandomNumber(min, max),
});
}
The code above is as far as I got, it works but surely there is a way to loop over the different properties in this.state
and call the function for each item?