我正在使用 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/