当前位置: 首页 > 知识库问答 >
问题:

太多的重放。React限制呈现次数以防止无限循环。本地反应

龙令
2023-03-14

当GetCreatorLicense()被禁用时,programe运行良好,我试图在循环之外设置数据,但仍然得到‘错误:太多的重新呈现’。React限制呈现的次数,以防止无限循环。错误,然后我把这个函数放在useEffect下,我只得到控制台输出,但useState没有改变

        const Getdata = async () => {
            let grabbedData = [];
            await firebase
              .database()
              .ref(`/users`)
              .orderByKey()
              .on("value", (snapshot, key) => {
                // console.log('snapshot....', snapshot);
                grabbedData.push(snapshot.val());
              });
            setUserdata(grabbedData);
            //   console.log('grabbedData....', grabbedData);
          };
        
          useEffect(() => {
            Getdata();
            // GetCreatorLicense();
          }, []);
        
  React.useEffect(() => {
    GetCreatorLicense();
  }, []);

  const GetCreatorLicense = () => {
    console.log("this is a GetCreatorLicense this got printted");
    let checkDis = false;
    let checkDealer = false;
    let checkSubDis = false;
    let checkMoneyCollector = false;
    let checkfranchise = false;
    Object.keys(userdata).map(function (key) {
      console.log("I am not printed", userdata[key]);
      let x = userdata[key];
      Object.keys(x).map(function (key2) {
        console.log("I am not printed either", x[user1.uid].create.distributor);
        if (x[user1.uid].create.distributor) {
          checkDis = true;
        }
        if (x[user1.uid].create.subDistributor) {
          checkSubDis = true;
        }
        if (x[user1.uid].create.delaer) {
          checkDealer = true;
        }
        if (x[user1.uid].create.moneyCollector) {
          checkMoneyCollector = true;
        }
        if (x[user1.uid].create.franchise) {
          checkfranchise = true;
        }
      });
    });
    if (checkDis) {
      setCanCreateDistributor(true);
    }
    if (checkSubDis) {
      setCanCreateSubDistributor(true);
    }
    if (checkDealer) {
      setCanCreateDealer(true);
    }
    if (checkMoneyCollector) {
      setCanCreateMoneyCollector(true);
    }
    if (checkfranchise) {
      setCanCreateFranchise(true);
    }
  };

共有1个答案

干弘深
2023-03-14

您确实在useEffect中更改了状态。getData是从useeffect中调用的,它正在调用setuserdata,我认为这是一个usestate函数。

可以通过声明应该触发useEffect的状态变量来避免这种情况,确保这些状态在useEffect中不会被更改。

 类似资料: