How to Navigate from a functional component, from a screen in a tab navigator, where that tab navigator is nested in a parent stack navigator

前端 未结 1 459
情深已故
情深已故 2021-01-26 08:32
HomeStackNavigator(stack)
---HomeTabsNavigator(Tab)
   ---FirstTab(stack)
      ---CreatePost(screen)
      ---Posts(Tab)
         ---Following(screen)
         ---Feed(         


        
1条回答
  •  情歌与酒
    2021-01-26 09:12

    You can use the RootNavigation approach: https://reactnavigation.org/docs/navigating-without-navigation-prop/

    First you create a file at your directory root called RootNavigation.js that looks like this:

    import * as React from 'react';
    
    export const navigationRef = React.createRef();
    
    export function navigate(name, params) {
      navigationRef.current?.navigate(name, params);
    }
    

    Then you pass the navigationRef as a ref to your NavigationContainer:

    import * as RootNavigation from './RootNavigation';
    // ...
    
       
    
    

    This allows you to navigate from anywhere.

    Then you can do something like this in your Feed screen:

    const Feed = () => {
        // ...
        

    0 讨论(0)
提交回复
热议问题