首页 > 解决方案 > 解决承诺 React Native

问题描述

我想在进入主屏幕之前执行一系列调度操作。

我觉得promise 可能是解决这个问题的正确方法,但是使用下面的代码,我们在完成每个功能(自动登录、工单、联系人、警报、合作伙伴)之前,向下走代码并更改屏幕。如何确保在最后一个 resetNavigation 命令之前完成了这 5 个操作?

Promise.resolve ( this.store.dispatch(autoLogin())  )
 .then(function (response) {
   store.dispatch(tickets());
 })
 .then(function (response) {
   store.dispatch(contacts());
 })
 .then(function (response) {
   store.dispatch(alerts());
 })
 .then(function (response) {
   store.dispatch(partners());
 })
 .then(function (response) {
   this.resetNavigation('HomeScreen');
 })

标签: javascriptreact-native

解决方案


您可以在您的 components.And 中的合作伙伴操作影响的 shouldComponentUpdate() 方法中分派一个操作以再次导航。举个例子:

class ComponentEffectedByPartnerAction {
     shouldComponentUpdate(){
       this.resetNavigation('HomeScreen');
     }
}

并删除你承诺的最后一步

Promise.resolve ( this.store.dispatch(autoLogin())  )
 .then(function (response) {
   store.dispatch(tickets());
 })
 .then(function (response) {
   store.dispatch(contacts());
 })
 .then(function (response) {
   store.dispatch(alerts());
 })
 .then(function (response) {
   store.dispatch(partners());
 })
 .then(function (response) {
   console.log("last chance")
   //this.resetNavigation('HomeScreen');
 })

推荐阅读