Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initial route params in React Navigation v5?

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 ?
        ...
    );
}
like image 255
Denis Kulagin Avatar asked Feb 07 '20 14:02

Denis Kulagin


1 Answers

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'
like image 110
Denis Kulagin Avatar answered Sep 23 '22 00:09

Denis Kulagin