【发布时间】:2019-01-27 20:17:04
【问题描述】:
ReactJS 新手。
我正在尝试构建一个在容器周围移动一些组件的小组件。这个想法是用户点击一个按钮,div的位置发生变化。
我尝试使用Object.keys 和Object.entries,它们都不起作用。我试图用this.state 创建一个数组,这样我就可以做array.map() 但它不起作用。
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),
});
}
据我所知,上面的代码可以工作,但肯定有一种方法可以遍历this.state 中的不同属性并为每个项目调用函数?
【问题讨论】:
标签: javascript arrays reactjs state