随机化结果 onload React
Randomize results onload React
我试图在每次刷新页面时随机化从 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);
}
);
});
您将不得不创建一个函数来随机播放它们。
我从这条评论中得到了这个
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
你会想在这里调用它
getMarvelResponse = (id, name, offset) => {
getCharacters({
id: id,
name: name,
offset: offset,
}).then(({ characters }) => {
/////////Call Here
this.setState(
{
characters: characters.filter(
(chr) => !chr.thumbnail.path.match(imageFound)
),
selectedCard: null,
offset: offset,
name: name,
},
() => {
console.log(this.state.characters);
}
);
});
我试图在每次刷新页面时随机化从 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);
}
);
});
您将不得不创建一个函数来随机播放它们。 我从这条评论中得到了这个
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
你会想在这里调用它
getMarvelResponse = (id, name, offset) => {
getCharacters({
id: id,
name: name,
offset: offset,
}).then(({ characters }) => {
/////////Call Here
this.setState(
{
characters: characters.filter(
(chr) => !chr.thumbnail.path.match(imageFound)
),
selectedCard: null,
offset: offset,
name: name,
},
() => {
console.log(this.state.characters);
}
);
});