即使循环后数组也为空

问题描述 投票:0回答:1

我正在尝试用数据库中的某些寄存器填充数组。但是,即使在检索了所述寄存器并将它们放入数组后,循环后数组仍为空]

const userParties = [];

userFollowing.forEach(element => {
  // Here's when I gather the information that I need
  dispatchGetUserEvents(element.user, response => {
    userParties.push(response);
    console.log(userParties); // Here the array is full of elements, like it's supposed to be
  });
});

console.log(userParties); // However, here 'userParties' return '[]'
this.setState({ followUsersEvents: userParties });
this.setState({ userImage: userImg });

我试图在循环上更新状态数组,但是我也没有运气。'userParties'不是状态数组。

reactjs react-native
1个回答
0
投票
const userParties = [];

userFollowing.forEach(element => {
  // Here's when I gather the information that I need
  dispatchGetUserEvents(element.user, response => {
    userParties.push(response);
    console.log('dispatchGetUserEvents', userParties); // Here the array is full of elements, like it's supposed to be
  });
});

console.log('outside', userParties); // However, here 'userParties' return '[]'
this.setState({ followUsersEvents: userParties });
this.setState({ userImage: userImg });
© www.soinside.com 2019 - 2024. All rights reserved.