【问题标题】:How could I create a function with a completion handler in React-native?如何在 React-native 中创建一个带有完成处理程序的函数?
【发布时间】:2018-09-22 11:00:00
【问题描述】:

我正在尝试在 react-native 中使用完成处理程序,这可能吗? 请一些样品会有所帮助。 谢谢。

所以我在父类的实例类中调用此方法,但希望该方法在完成后返回一个值。
现在它在创建值之前返回,但希望值在创建后传递。

getfcmToken() {
   //generate token here
   firebase.messaging().getToken().then(token => {
       if (token) {
           console.log("LOG: ", token);
           _userID = token
           alert(_userID)
        } else {
            // user doesn't have a device token yet
        }
    })
    return this._userID;
}

【问题讨论】:

  • 你的意思是像回调系统?如是;是的,你可以,但是在完成什么时回调?

标签: react-native cross-platform reactive-programming


【解决方案1】:

我认为您可以将一个函数作为参数发送给 getfcmToken。例如:

在帮助器/实例类中

getfcmToken(callback) {
   firebase.messaging().getToken().then(token => {
       if (token) {           
           this._userID = token;
           callback({success: true, token: token};
        } else {
            // user doesn't have a device token yet
            callback({success: false, message: 'User doesn't have device token'});
        }
    });
}

然后在父类中:

getUserToken() {
   firebaseHelper.getfcmToken((result)=>{
     if (success === true) {
       //do something with token
     }
   });
}

【讨论】:

    猜你喜欢
    • 2015-08-04
    • 2020-04-10
    • 1970-01-01
    • 2021-11-06
    • 2021-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多