I have the following Tab Navigator in my React Native app, using React Navigation v6:
<Tab.Navigator>
<Tab.Screen name="Screen1" component={Screen1}/>
<Tab.Screen name="Screen1" component={Screen2}/>
</Tab.Navigator>
I want to pass a prop called prop1
that will be accessible in each screen. How do I do this?
2
Answers
React Navigation v6 doesn’t allow you to pass props directly to screens from the navigator component as you could in previous versions. However, there are still a few different methods to pass props to screens.
You can wrap your navigator with a context provider and use the useContext hook in your screen components to access your data. Like so:
In your Screen1 and Screen2 components:
Alternatively, you can use Higher Order Components:
You can wrap your Screen components in a higher-order component, and pass the props to the HOC. Like so:
Screen1 and Screen2 components:
You can do this by moving the component from the
component
prop to become a child of the screen. Example:Official doc here