【发布时间】:2017-07-11 23:22:24
【问题描述】:
我想使用反应导航中的后退操作来关闭模式,如下所示:
this.props.navigation.dispatch(NavigationActions.back({
key: this.props.navigation.state.params.previousKey
}));
导航到模态时我通过了上一屏的键:
this.props.navigation.navigate('ModalScreen', {
previousKey: this.props.navigation.state.key
});
但什么也没发生。
当我不使用键时,navigationaction.back 会在通过单击按钮调用该函数时正确地解除模式返回到上一个屏幕。
但是当我从构造函数中调用它时,它会返回到根屏幕...
有什么想法吗?
编辑 1
尝试使用以下自定义操作的另一种方法。当我重新加载应用程序并尝试它时,它第一次正常工作。但是当我第二次尝试而不重新加载应用程序时,我得到了错误:无法读取未定义的属性“路由”。
在我的 router.js 文件中:
const CheckInStack = StackNavigator({
CheckIn: {
screen: CheckIn,
navigationOptions: {
header: null,
headerLeft: null,
gesturesEnabled: false,
}
}
});
export const SiteStack = StackNavigator({
Site: {
screen: Site,
navigationOptions: {
header: null,
headerLeft: null,
gesturesEnabled: false,
}
},
CheckIn: {screen: CheckInStack,},
}, {mode: 'modal', headerMode: 'none'});
const prevGetCheckInStateForAction = SiteStack.router.getStateForAction;
SiteStack.router = {
...SiteStack.router,
getStateForAction(action, state) {
if(state && action.type === "DISMISS_MODAL") {
console.log('in router');
const routes = state.routes.slice();
routes.pop();
return {
...state,
routes,
index: routes.length -1,
}
}
return prevGetCheckInStateForAction(action, state);
}
};
在我的 screen.js 文件中:
componentDidMount() {
const {profile} = this.props.auth;
var channel = pusher.subscribe('private-user.' + profile.id );
channel.bind('App\\Events\\Order\\SiteHasAnsweredCheckIn', event => {
// this.props.navigation.dispatch(NavigationActions.back()); => this is where the back actions is launched. nowhere else.
//CUSTOM ACTION DISPATCH
this.props.navigation.dispatch({
type: 'DISMISS_MODAL'
});
});
}
【问题讨论】:
标签: react-native react-navigation