Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing params to tab navigator React Navigation 5

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 tab navigator with 2 screens: Posts and Users. These two screen both depend on params passed from List. However, i am only able to pass params to one of the screens using this method:

navigation.navigate('PostsTabNav', {
  params: {
    network: item,
  },
  screen: 'NetworkPosts' //or NetworkUsers
});

I have tried to pass the params to my navigator directly by doing this:

navigation.navigate('PostsTabNav', {
  network: item
});

The first option only allows me to pass to one screen. The second option allows me to access the params inside the navigator like this:

const PostsTabNav = createMaterialTopTabNavigator();
const PostsMainNav = (props) => {
    const temp = props.route.params.network; //params here

    return (
        <PostsTabNav.Navigator>
            <PostsTabNav.Screen name="NetworkPosts" component={NetworkPostsScreen} />
            <PostsTabNav.Screen name="NetworkUsers" component={NetworkUsersScreen} />
        </PostsTabNav.Navigator>
    );
};

Is there a way to pass temp to both my screens? If not is there a better way to handle this situation?

Here's the code for the StackNavigator

const NetworkListStackNav = createStackNavigator();
export const NetworksListNavigator = () => {
    return (
        <NetworkListStackNav.Navigator>
            <NetworkListStackNav.Screen name="List" component={ListScreen} />
            <NetworkListStackNav.Screen name="PostsTabNav" component={PostsMainNav} />
        </NetworkListStackNav.Navigator>
    );
};
like image 848
abdi Avatar asked Apr 16 '20 19:04

abdi


People also ask

How do I pass props with navigate?

Pass data in route state on the Navigate component's state prop. And to access the route state on the target routed component use the useLocation hook access it.

How do you pass parameters in react?

To pass an event and parameter onClick in React:Pass an inline function to the onClick prop of the element. The function should take the event object and call handleClick . Pass the event and parameter to handleClick .

How to use React context with pass params in Navigator?

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:

How to implement stacknavigator in React Native?

To implement the stack navigator in React Native, we need to install the StackNavigator package. Now, we create Blog and Blog Detail screens and configure the navigation between these screens in a while. Go to terminal and run the following command to generate the screens. Open screen/Blog.js file and add the following code.

How do I pass a route parameter to a navigation object?

There are two pieces to this: Pass params to a route by putting them in an object as a second parameter to the navigation.navigate function: navigation.navigate ('RouteName', { /* params go here */ }) Read the params in your screen component: route.params. We recommend that the params you pass are JSON-serializable.

How to install Expo on react with react navigation?

Allow Expo to be installed by selecting “Y” and don’t forget to download the Expo app in your mobile device based on the platform (iOS or Android). Go inside the project directory. Next, Install the required React Navigation packages. Also, Install the following dependencies that is essential for navigators.


1 Answers

You can set initial params to your screens.

const PostsTabNav = createMaterialTopTabNavigator();
const PostsMainNav = (props) => {
    const temp = props.route.params.network
    return (
        <PostsTabNav.Navigator>
            <PostsTabNav.Screen name="NetworkPosts" component={NetworkPostsScreen} initialParams={network:temp}/>
            <PostsTabNav.Screen name="NetworkUsers" component={NetworkUsersScreen} initialParams={network:temp}/>
        </PostsTabNav.Navigator>
    );
};
like image 81
Garvit Tyagi Avatar answered Nov 16 '22 02:11

Garvit Tyagi