skip to Main Content

In ‘react-navigation’ library, How can I send the parameters with initialRouteName in Stack.Navigator.

 return (
    <Stack.Navigator
      initialRouteName={routeName}
      screenOptions={{
        animation: 'slide_from_right',
      }}>

This is the return of my navigation file and I’m setting the routeName dynamically but the screen on which I want to initially route must need route parameters to pass.

2

Answers


  1. you can utilize the prop "initialParams" that can be added to your screen component

    <Screen 
       initialParams={{id:0}}
       {...otherScreenProps}
    />
    

    you can also handle it with a condition like initialParams={initialRouteName === screenName ? {id:0} : undefined}

    I believe there are other options like setParams, but i’m unsure of your exact use case

    Login or Signup to reply.
  2. you can pass params as below.
    initialParams={{ itemId: 42 }}

    These initialParams props are provided by react-navigation, you can use that initially.

    also whenever you setparams from the specific screen you can use the code below.

    navigation.setParams({
      itemId: {set your dynamic value},
    });
    

    for the reference :-
    click here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search