ios - React Native - 在异步操作后导航

标签 ios reactjs react-native redux react-router-redux

我正在使用 React Native 和 Redux 开发移动应用程序,我遇到了软件设计问题。 如果该操作成功,我想调用 REST API(异步操作)进行登录并导航到主视图。 我正在使用 redux 和 thunk,所以我已经实现了异步操作,所以我的主要疑问是:我应该将导航到主视图的逻辑放在哪里?

我可以直接从操作访问导航器对象并在那里执行导航吗? 我应该在登录组件中这样做吗? (正如我已经在做的那样 - 检查下面的代码)。

componentWillReceiveProps(nextProps){
    if(nextProps.errorLoginMsg){
        Alert.alert("Login Failed", nextProps.errorLoginMsg);
    }
    else if(!nextProps.user.isNull()){
      this.props.navigator.replace({name: 'main'});
    }
  }

我不确定组件中是否包含该逻辑。似乎不是一个好习惯。还有其他方法吗?

谢谢

最佳答案

下面是我的代码:

                const resetAction = NavigationActions.reset( {
                    index  : 0,
                    actions: [
                        NavigationActions.navigate( { routeName: 'Home' } )
                    ]
                } );

                this.props.requestDeleteEvent( {
                    id: eventID
                } ).then( () => {
                    this.props.navigation.dispatch( resetAction );
                } );

在函数 requestDeleteEvent 中:

export function requestDeleteEvent(requestData) {
    const id = requestData.id;

    return (dispatch, getState) => {
        return fetch(Config.event + id, {
            method: 'DELETE',
            headers: {
                'Content-Type': 'application/json; charset=UTF-8',
            },
        })
            .then((response) => getResponseJson(response))
            .then((responseJson) => {
                    if (responseJson.isSuccess) {
                        return dispatch(deleteEvent(id));
                    } 
                    else {
                        return dispatch(triggerToast({type: 'alert', content: ERROR}));
                    }
                }
            );
        }
    }

关于ios - React Native - 在异步操作后导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36238034/

相关文章:

javascript - 使用 Jest 测试 firebase 云消息传递

ios - 即使 allowsEditing 为真,editingInfo 中也没有 UIImagePickerControllerEditedImage

javascript - 替换一个对象内的钩子(Hook) useState

reactjs - 如何迁移到react-redux-firebase v3

reactjs - 如何在不导航到子组件的情况下发送值

javascript - 如何使用 Firebase 刷新 token 保持用户身份验证?

ios - 沿着贝塞尔路径拖动 UIView

ios - 迭代 Swift 扩展中的可用类?

ios - 是否需要 Apple 开发者 ID 来自动化 iOS 设备?

javascript - 如何使用 reactjs 使用 scrollintoview 方法将列表项滚动到 View 中?