简体   繁体   English

map function里面如何使用useEffect?

[英]How to use useEffect inside map function?

I have two tables in Firebase: Vouchers & ClaimedVouchers.我在 Firebase 中有两张表:Vouchers & ClaimedVouchers。 I am trying to display the vouchers that do not appear in the ClaimedVouchers table.我正在尝试显示未出现在 ClaimedVouchers 表中的凭证。 So, I have a query that gets all of the vouchers, then another that checks if they're claimed and if it's claimed or not the function should return either true or false:所以,我有一个获取所有凭证的查询,然后是另一个检查它们是否被认领以及是否被认领的查询 function 应该返回 true 或 false:

This is isClaimedAPI.js这是 isClaimedAPI.js

    export default () => {
      var result;
    
      async function isClaimed(voucher, user) {
        var db = Firebase.firestore();
        console.log("voucher");
        await db
    .collection("ClaimedVoucher")
    .where("voucherID", "==", voucher)
    .where("userID", "==", user)
    .get()
    .then(function (querySnapshot) {
      if (querySnapshot.empty === false) {
        result = true;
      } else {
        result = false;
      }
    })
    .catch(function (error) {
      console.log("Error getting documents: ", error);
    });
  console.log(result, "this is result");

  return result;
      //Call when component is rendered
      useEffect(() => {
        isClaimed().then((result) => setResult(result));
      }, []);
    
      return [isClaimed];

    
 

And then in my main function:然后在我的主要 function 中:

 var user = Firebase.auth().currentUser;
    var uid = user.uid;
    const [getVouchers, voucherList, errorMessage] = getVouchersAPI(ID); //List of vouchers to be checked
    const [isClaimed] = isClaimedAPI();
    
    
    return(
<ScrollView>
    {voucherList.map((item, index) => {
              var voucher = item;
              
              var isVoucherClaimed = isClaimed(voucher.voucherID, uid);
              console.log("this is result, ", isVoucherClaimed);
              if (
                isVoucherClaimed === false
              ) {
                  return <Text>{item.name}<Text>
                }
    })}
  </ScrollView>
 );

Now nothing happens and I receive the following warning: [Unhandled promise rejection: FirebaseError: Function Query.where() requires a valid third argument, but it was undefined.] but I think this is unrelated to the issue.现在什么都没有发生,我收到以下警告:[未处理的 promise 拒绝:FirebaseError:Function Query.where() 需要有效的第三个参数,但它是未定义的。] 但我认为这与问题无关。

Your isClaimed is an async function, meaning that it returns a promise - or a delayed result.您的isClaimedasync function,这意味着它返回 promise - 或延迟结果。 If you want to wait for the result when calling isClaimed , you'll need to use await :如果您想在调用isClaimed时等待结果,则需要使用await

await isClaimed(voucher.voucherID, uid);
console.log(result);

This most likely isn't possible in a render method though, which is why (as Asutosh commented) you'll have to store the result in the state, and then use the state in your render method.不过,这很可能在渲染方法中是不可能的,这就是为什么(正如 Asutosh 评论的那样)您必须将结果存储在 state 中,然后在渲染方法中使用 state。

So the setup you need is:所以你需要的设置是:

  • Start the loading of all your data in componentDidMount or with useEffect .componentDidMount或使用useEffect开始加载所有数据。
  • When the data is loaded, put it in the state with setState or a state hook.加载数据后,将其放入带有setState或 state 挂钩的 state 中。
  • Use the data in your render method.在您的渲染方法中使用数据。

For a few examples of this, see:有关这方面的一些示例,请参见:

Just to highlight how you can use isClaimed as hoook and set state calling a async function inside it.只是为了强调如何使用 isClaimed 作为钩子并在其中设置 state 调用异步 function 。 Later use the above hook in a react component.稍后在反应组件中使用上述钩子。 Please follow below sanbox.请按照下面的 sanbox。

https://codesandbox.io/s/confident-cray-4g2md?file=/src/App.js https://codesandbox.io/s/confident-cray-4g2md?file=/src/App.js

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

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