【发布时间】:2021-06-29 21:13:30
【问题描述】:
每次刷新页面时,我都会尝试随机化从 API 收到的结果。我有点卡在设置 onload 的方式上。
这是我的代码
handleRandom = (e) => {
this.getMarvelResponse(null, null, e);
};
componentDidMount() {
const random = Math.floor(Math.random() * 1473);
this.getMarvelResponse(null, null, this.state.offset);
onload = () => {this.handleRandom(random)}
}
getMarvelResponse = (id, name, offset) => {
getCharacters({
id: id,
name: name,
offset: offset,
}).then(({ characters }) => {
this.setState(
{
characters: characters.filter(
(chr) => !chr.thumbnail.path.match(imageFound)
),
selectedCard: null,
offset: offset,
name: name,
},
() => {
console.log(this.state.characters);
}
);
});
【问题讨论】:
标签: reactjs random refresh onload