this.setState and following function are not even running

It’s not logging the item of "this.state.UsersAvatars[1]", nor the "check". it’s also not changing the array, the only item in UsersAvatars is the first object

Can I please ask for your help? (:

  newUserTypeAvatarsArray = async () => {
    const avatars = this.state.avatars.map((avatar, index) => ({
      avatar: avatar,
      isChcecked: false,
      index: index,
    }));
    console.log(avatars);
    let singleUser = {
      userType: "everyone",
      isChecked: false,
      avatars: avatars,
    };
    await this.setState(
      {
        UsersAvatars: [singleUser],
      },
      () => {
        console.log(this.state.UsersAvatars[0]);
      }
    );

    await this.state.users_types.map(async (userTypee, index) => {
      console.log("in loop users_types");
      const temporaryObject = {
        userType: userTypee,
        isChecked: false,
        avatars: avatars,
      };
      let temporaryArray = await [...this.state.UsersAvatars, temporaryObject];
      await this.setState(
        (prevState) => (
          {
            UsersAvatars: temporaryArray,
          },
          function () {
            console.log("check");
            console.log(this.state.UsersAvatars[1]);
          }
        )
      );
    });
    await this.setState({
      UsersAvatarsLoading: false,
    });
  };

5 thoughts on “this.setState and following function are not even running”

Leave a Comment