const getChoices = async () => {
try {
setIsLoading(true);
for (let i = 0; i < 1387; i++) {
for (let j = 0; j < days.length; j++) {
if (
moment(data[i].date.start).utc().format("MM/DD/YYYY") === days[j]
) {
const kli = await Contract.methods
.GetChoice(data[i].id)
.call({ from: Account[0] });
console.log(kli);
if (kli === "0") {
// console.log(data[i]);
} else {
if (parseInt(kli) === data[i].teams.home.id) {
console.log("inserted in home");
console.log(data[i].id, data[i].teams.home.nickname);
setChoices((oldState) => [
...oldState,
{
MatchID: data[i].id,
BetTeamName: data[i].teams.home.nickname,
},
]);
} else if (parseInt(kli) === data[i].teams.visitors.id) {
console.log("inserted in vistiroes");
setChoices((oldState) => [
...oldState,
{
MatchID: data[i].id,
BetTeamName: data[i].teams.visitors.nickname,
},
]);
}
}
}
}
}
console.log(choices[1]);
setChoices([...new Set(choices)]);
console.log(choices[0]);
} finally {
setIsLoading(false);
}
};
以下是用于附加每个用户的选择的异步函数。它被称为UsEffect。UseState未更新变量的状态。
这是控制台日志
10PublicPools.js:56 0
PublicPools.js:56 25
PublicPools.js:71 inserted in vistiroes
PublicPools.js:56 26
PublicPools.js:61 inserted in home
PublicPools.js:62 11027 'Magic'
PublicPools.js:56 21
PublicPools.js:71 inserted in vistiroes
10PublicPools.js:56 0
PublicPools.js:56 19
PublicPools.js:71 inserted in vistiroes
PublicPools.js:56 25
PublicPools.js:71 inserted in vistiroes
PublicPools.js:56 14
PublicPools.js:71 inserted in vistiroes
PublicPools.js:56 7
PublicPools.js:71 inserted in vistiroes
7PublicPools.js:56 0
PublicPools.js:84 undefined
PublicPools.js:86 undefined
UseState目前在那里工作得很好,但开始变得奇怪,我尝试了几个解决方案,但仍然面临同样的问题
1条答案
按热度按时间eqqqjvef1#
SetChoices会导致您的组件一次又一次地重新呈现,因为它在循环中,或者您可以在临时变量中赋值结果,然后仅在循环结束后调用setChoices一次。