Initial route params in React Navigation v5?

笑着哭i 提交于 2021-01-27 06:38:29

问题


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 ?
        ...
    );
}

回答1:


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'


来源:https://stackoverflow.com/questions/60115818/initial-route-params-in-react-navigation-v5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!