美文网首页
react-navigation StackNavigator

react-navigation StackNavigator

作者: 陈广天 | 来源:发表于2018-06-04 14:48 被阅读0次

https://reactnavigation.org/docs/en/navigation-prop.html#reset

應用場景:

由Page1跳轉到Page2,在Page2再跳轉到Page3,在Page3返回時直接回到Page1

實現:Page1頁面navigate跳轉至Page2,在Page2跳轉至Page3時重置路由
//路由配置
import { createAppContainer } from "react-navigation"
import { createStackNavigator } from "react-navigation-stack"
import Page1 from "xxx"
import Page2 from "xxx"
import Page3 from "xxx"
const AppNavigator = createStackNavigator({
    Page1: {
        screen: Page1,
    },
    Page2: {
        screen: Page2,
    },
    Page3: {
        screen: Page3,
    }
});
export default createAppContainer(AppNavigator);


//Page1.js
this.props.navigation.navigate("Page2") //導航至Page2


//Page2.js
import { StackActions, NavigationActions } from "react-navigation"
const resetAction = StackActions.reset({
     index: 1,
     actions: [
        NavigationActions.navigate({ routeName: 'Page1' }), 
        NavigationActions.navigate({ routeName: 'Page3', params: {}})
      ]
});
this.props.navigation.dispatch(resetAction) //導航至Page3,重置路由

參考:https://reactnavigation.org/docs/en/stack-actions.html#reset

相关文章

网友评论

      本文标题:react-navigation StackNavigator

      本文链接:https://www.haomeiwen.com/subject/ehxysftx.html