Passing params to tab navigator React Navigation 5

前端 未结 1 1640
自闭症患者
自闭症患者 2021-01-18 00:28

I’m using materialTopTabs and it seems like this loads all the screens in the navigator once its mounted. I have a screen List and inside it a

相关标签:
1条回答
  • 2021-01-18 01:06

    Pass params to the navigator and then expose it to the tabs using React Context.

    Create a context in a separate file which you can import in both your navigator and screens:

    export const NetworkContext = React.createContext();
    

    Then provide the params in the context:

    const PostsTabNav = createMaterialTopTabNavigator();
    
    const PostsMainNav = ({ route }) => {
      return (
        <NetworkContext.Provider value={route.params.network}>
          <PostsTabNav.Navigator>
            <PostsTabNav.Screen name="NetworkPosts" component={NetworkPostsScreen} />
            <PostsTabNav.Screen name="NetworkUsers" component={NetworkUsersScreen} />
          </PostsTabNav.Navigator>
        </NetworkContext.Provider>
      );
    };
    

    In your screen component, use the context:

    const network = React.useContext(NetworkContext);
    

    Also see https://reactnavigation.org/docs/hello-react-navigation#passing-additional-props

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