【问题标题】:How to execute code after another part is completed EXPO REACT NATIVE另一部分完成后如何执行代码 EXPO REACT NATIVE
【发布时间】:2021-03-02 23:28:52
【问题描述】:

我正在尝试将记录保存到数据库中,如果该记录不在用户的个人资料中(他没有发现这个地方),而且该记录存在于所有地方的集合中。

我正在使用 Expo react native,我认为我的问题是,如果条件将在函数 recordInUsersAccountrecordInGlobalDatabase 之前执行。完成这两个功能后,有什么方法可以确保执行?在函数中,我在 state={} 中重写变量,所以我可以在下面检查它们。 (我尝试了 .then() 和 await, async 但我没有成功)。

非常感谢。

saveScannedQrCode(idOfPlace) {

     this.recordInUsersAccount(idOfPlace);
     this.recordInGlobalDatabase(idOfPlace);

    if (!this.state.placeAlreadyScanned && this.state.placeExistInDatabase) {  
      // we will add this record into database
    } else {      
          // we will NOT add this record into database
  }
 }

这是函数的代码:

recordInUsersAccount(idOfPlace) {
        const userId = auth.currentUser.uid;
        const usersRef = db
          .collection("placesExploredByUsers") // default
          .doc("mUJYkbcbK6OPrlNuEPzK") // default
          .collection("s53sKFeF5FS0DjuI2cdO1Rp9sCS2") // uid
          .doc(idOfPlace); // id of place
    
        usersRef.get().then((docSnapshot) => {
          if (docSnapshot.exists) {
            this.setState({
              placeAlreadyScanned: true, // place is in user's database
            });
          } else {
            this.setState({
              placeAlreadyScanned: false, // place is NOT in user's database
            });
          }
        });
      }
      recordInGlobalDatabase(idOfPlace) {
        const usersRef = db
          .collection("databaseOfPlaces") // default
          .doc(idOfPlace); // id of place
    
        usersRef.get().then((docSnapshot) => {
          if (docSnapshot.exists) {
            this.setState({
              placeExistInDatabase: true, // place is in global database of places
            });
          } else {
            this.setState({
              placeExistInDatabase: false, // place is NOT in global database of places
            });
          }
        });
      }

【问题讨论】:

  • 你能发布你想要等待的函数的代码吗?
  • 当然,我更新了我的帖子

标签: react-native expo


【解决方案1】:

代码的问题是 React 中的 setState 是异步的,并且您尝试在执行修改状态的函数后直接检查值。

假设您的方法运行良好并且执行了他们应该做的事情,您可以执行以下操作:

  1. 让你的方法保持原样,修改状态。

  2. 像现在一样调用saveScannedQRCode,触发两个辅助方法。

  3. 您可以在 componentDidUpdate 生命周期挂钩中执行此操作,而不是在调用它们后立即检查状态。

     componentDidUpdate(prevProps, prevState) {
       if (prevState.placeExistInDatabase !== this.state.placeExistInDatabase && prevState.placeAlreadyScanned !== this.state.placeAlreadyScanned) {
         // do something here - at this point state is already updated and ready to use.
         // you could check for the values you're waiting for to update the DB, else not do anything.
       }
     }
    
      saveScannedQrCode(idOfPlace) {
        this.recordInUsersAccount(idOfPlace);
        this.recordInGlobalDatabase(idOfPlace);
      }
    

一件事 - 确保在处理更新后重置状态(例如,将其设置为 null),这样您的 componentDidUpdate 挂钩不会有任何问题,并且您的严格相等会很好。

【讨论】:

  • 如果我像这样实现它,但似乎它不起作用:我正在获取变量的默认值,或者我被警报“发送垃圾邮件”。也许我应该提供整个代码作为扫描二维码后应该触发的函数?
  • 通过|| 更改您的条件,以便它随时检查(如果您的请求在不同时间返回,那么我提出的条件可能不正确)然后进行检查。否则,您可以使用setState 的回调。
  • 我试着把 ||条件,但在这种情况下,只有在函数 saveScannedQrCode 中调用的第一个方法会将其状态从 null 更改为 true/false,第二个方法将保持为 null。如果我将条件保持原样(&&),它甚至不会打印我放在 componentDidUpdate 中的警报以检查它是否正常工作。
  • 嘿,所以我设法让它运行起来。我使用了您关于 componentDidUpdate 的建议,但我检查了两个变量(placeAlreadyScaned 和 placeExistsInDatabase)是否不为空,然后执行操作。所以谢谢你的建议:)
猜你喜欢
  • 2018-01-26
  • 2019-11-20
  • 2013-08-21
  • 2013-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-23
  • 1970-01-01
相关资源
最近更新 更多