简体   繁体   English

在React Native / Redux中处理异步请求

[英]Handle async requests in react native / redux

I'm using firebase to build an app with react native. 我正在使用Firebase来构建带有本机响应的应用程序。 I'm struggling with how I should structure my redux actions within my app. 我在如何在我的应用程序中构造我的redux动作方面苦苦挣扎。

As is stands I have a function that will fetch a users profile. 就目前而言,我有一个将获取用户个人资料的功能。 I make a reference to the firebase firestore and then use then and catch statements to resolve the promise returned. 我引用了Firebase Firestore,然后使用then和catch语句来解决返回的诺言。 Here is where my problem comes into it. 这是我的问题所在。

return(dispatch){
dispatch({type: GET_PROFILE})
var docRef = firebase.firestore().doc('users/' + userUid);
docRef.get()
.then(() => {
    dispatch({type: GET_PROFILE_SUCCESS})
})
.catch(() => {
   dispatch({type: GET_PROFILE_FAIL})
})
}

I am dispatching the action GET_PROFILE so that when my render function is called in my profile screen, it will load a spinner while the user waits. 我正在分派动作GET_PROFILE,以便在个人资料屏幕中调用我的渲染函数时,它将在用户等待时加载微调器。 The problem I've got is that if the user isn't connected to the internet. 我遇到的问题是,如果用户未连接到互联网。 When they make the request, the action is sent to redux but GET PROFILE SUCCESS / FAIL is never dispatched, as having no internet connection isn't an error 当他们发出请求时,该操作将发送到redux,但永远不会调度GET PROFILE SUCCESS / FAIL,因为没有互联网连接不是错误

As a result all the user sees is a spinner until they restart the app. 结果,所有用户看到的都是微调器,直到他们重新启动应用程序为止。 I basically want to alert the user that they need a connection to the internet. 我基本上是想提醒用户他们需要连接到互联网。 I'm new to react native, Could someone advise me what's the best way to do this in react native? 我是本机反应的新手,有人可以建议我以本机反应的最佳方法是什么?

Thanks 谢谢

You can use this piece of code for network fail condition 您可以将这段代码用于网络故障情况

return(dispatch){
    dispatch({type: GET_PROFILE})
    var docRef = firebase.firestore().doc('users/' + userUid);
    docRef.get()
    .then(() => {
        dispatch({type: GET_PROFILE_SUCCESS})
    }, 
      // ADD THESE LINES
      (error) => {
            dispatch({type: GET_PROFILE_FAIL})
          })
    .catch(() => {
       dispatch({type: GET_PROFILE_FAIL})
    })
    }

You can get network informations with NetInfo and store it in redux : 您可以使用NetInfo获取网络信息,并将其存储在redux中:

In your main component : 在您的主要组件中:

componentDidMount() {
    NetInfo.isConnected.fetch().then((isConnected) => {
      store.dispatch({ type: 'CHANGE_CONNECTION_STATUS', isConnected });
    });
    NetInfo.isConnected.addEventListener('connectionChange', this.handleConnectionChange);
}

componentWillUnmount() {
    NetInfo.isConnected.removeEventListener('connectionChange', this.handleConnectionChange);

handleConnectionChange = (isConnected: boolean) => {
    store.dispatch({ type: 'CHANGE_CONNECTION_STATUS', isConnected }));
}

Then you can wrap your request with an if statement to handle offline use cases : 然后,您可以使用if语句包装您的请求,以处理离线用例:

return(dispatch){
  if(store.getState().isConnected) {
    dispatch({type: GET_PROFILE})
    var docRef = firebase.firestore().doc('users/' + userUid);
    docRef.get()
    .then(() => {
        dispatch({type: GET_PROFILE_SUCCESS})
    })
    .catch(() => {
       dispatch({type: GET_PROFILE_FAIL})
    })
  } else {
    Alert.alert('You are offline');
  }
}

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

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