In the docs for React Navigation v6, in the Passing Additional Props section it says Sometimes we might want to pass additional props to a screen. We can do that with 2 approaches and the first approach it recommends is React Context. I've never used React Context, but the link says it's out of date. Is there a more up-to-date alternative to React Context? The other option it suggests is: Use a render callback for the screen instead of specifying a component prop.
Basically I want to do:
<Tab.Navigator>
<Tab.Screen prop1={prop1}>
</Tab.Navigator>
but I know you can't pass prop1 this way.
New documentation for React Context: useContext, Passing Data Deeply with Context. Please read React Navigation Passing parameters to routes and Initial params If you use
@react-navigation/bottom-tabsfor passing props directly you can use this way:createBottomTabNavigatororcreateMaterialBottomTabNavigator:In this example, the MyScreen component receives the additional props via the route.params object, similar to the previous example. You can set the initial params for the screen using the initialParams property. This sets the initial values of the additional props.
To access the additional props, you can use the options property of Tab.Screen and access
route.params?.additionalPropwithin the options function. Theroute.params?.additionalPropexpression ensures that the additionalProp is accessed safely even if it's not available. By following this approach, you can pass additional props to screens within tabs in React Navigation v6 and access them within the screen component.