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>
);
};
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.
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 .
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:
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.
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.
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.
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>
);
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With