Initial route params in React Navigation v5?
Asked Answered
I

1

16

React Navigation v3 was featuring an initialRouteParams property to pass initial values to this.navigation.props. Is there a way to set initial route params to be accessed via route.params in the React Navigation v5?

function MainScreen({route, navigation}) {
    return (
        // how to pass default values to route in here?
        // route.params.userParam ?
        ...
    );
}
Iniquitous answered 7/2, 2020 at 14:53 Comment(0)
I
27

It could be accomplished by passing initialParams to the Stack.Screen:

<Stack.Screen
    name="Main"
    component={MainScreen}
    initialParams={{ /* initialParams */ }}
/>

There is also a handy default syntax one can use (see No more getParam):

route.params?.userParam ?? 'defaultValue'
Iniquitous answered 8/2, 2020 at 5:41 Comment(2)
This does not seem to work when the "Main" screen is actually another Stack.Navigator. Then the params are not passed to the Screen rendered in this navigator.Attila
Of great help thanks.Extenuation

© 2022 - 2024 — McMap. All rights reserved.