简体   繁体   English

如何在 map function 在 useEffect 中运行后设置值

[英]how to set value after map function runs in useEffect

This is the real code here I am getting setOrder empty, I am trying keep away from map loop due infinite render how could pass grabbedData1 value to setOrder.这是真正的代码,我将 setOrder 设为空,我试图远离 map 循环,因为无限渲染如何将 grabbedData1 值传递给 setOrder。 I know setOrder gets value before.map runs.我知道 setOrder 之前获得了价值。map 运行。 I eneven tried to "customerList1".map in another function but i get infinite render error我什至尝试在另一个 function 中尝试“customerList1”.map 但我得到无限渲染错误

  const GetdataUsers = async () => {
    let grabbedData = [];
    console.log("setUserdata....", grabbedData.length);
    await firebase
      .database()
      .ref(`/users/${user1.uid}`)
      .orderByKey()
      .on("value", (snapshot, key) => {
        // console.log("snapshot....", snapshot);
        grabbedData.push(snapshot.val());
        setUserdata(grabbedData);
        console.log("setUserdata", grabbedData.length);
        if (grabbedData) {
          let customerList1 = [];
          firebase
            .database()
            .ref(`/serviceProvider/${user1.uid}/franchise/customers`)
            .orderByKey()
            .on("value", (snapshot) => {
              customerList1.push(snapshot.val());
              setCustomerList(customerList1);
              console.log("setCustomerList", customerList1);
              if (customerList1) {
                let grabbedData1 = [];
                Object.keys(customerList1).map(function (key) {
                  let y = customerList1[key];
                  return Object.keys(y).map(function (key2) {
                    let x = y[key2];
                    console.log("xxxxxxxxx", x);
                    firebase
                      .database()
                      .ref(`/orders/${x}`)
                      .orderByKey()
                      .on("value", (snapshot, key) => {
                        // console.log('snapshot....', snapshot);
                        grabbedData1.push(snapshot.val());
                      });
                  });
                });
                if (grabbedData1) {
                  setOrders(grabbedData1);
                  console.log("setOrders", grabbedData1.length);
                }
              }
            });
        }
      });
  };

  useEffect(() => {
    GetdataUsers();
  }, []);

I can't reproduce the code and test because of the Firebase but maybe you can try changing the scope of 'grabbedData1' to a wider scope and returning it from the function. I can't reproduce the code and test because of the Firebase but maybe you can try changing the scope of 'grabbedData1' to a wider scope and returning it from the function.

Possible solution:可能的解决方案:

const GetdataUsers = async () => {
    let grabbedData = [];
    let grabbedData1 = [];
    ...
    ...
    return grabbedData1;
}

useEffect(() => {
    const data = GetdataUsers();
    if(data)
        setOrders(data);
}, []);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM