Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React navigation undefined params

I'm trying to pass params into a new screen, and implemented it like mentioned here.

I have the following TouchableOpacity button.

<TouchableOpacity
  onPress={() => {
    this.props.navigation.navigate('SomeScreen', {
      title: 'Title',
      subTitle: 'Subtitle',
    });
  }}
>

On the other page (let's call it Somescreen), I have the following:

render() {
  const { navigation } = this.props;
  const title = navigation.getParam('title');
}

But title above is undefined:

{ params: undefined, routeName: "Somescreen", key: "id-xx" }

My rootStack:

const RootStack = createStackNavigator({
  SomescreenA: { screen: SomescreenA },
  SomescreenB: { screen: SomescreenB },
}, { headerMode: 'none' });

Why are my params undefined in a new screen?

like image 575
Kevin Etore Avatar asked Mar 27 '19 14:03

Kevin Etore


2 Answers

If you face a situation where your target screen get undefined params, probably you have a nested navigation stack.

Here you have to pass params to the navigate method in this way:

navigation.navigate('Root', {
  screen: 'Settings',
  params: { user: 'jane' },
});

For more information read this page in the official docs:

https://reactnavigation.org/docs/nesting-navigators/#navigating-to-a-screen-in-a-nested-navigator

like image 52
Ali Radmanesh Avatar answered Oct 01 '22 03:10

Ali Radmanesh


In my specific case, I was calling a nested navigator, so I had to manage how send those params to their specific screen, so I did this:

Send params this way...the regular way:

    navigation.navigate(
        'OrderNavigator',
        {itemSelected},
    );

Then, from navigator stack I did this:

const OrderNavigator = ({route: {params}}) => {
return (
    <Stack.Navigator initialRouteName="Order">
        <Stack.Screen name="Order" component={Order} options={{headerShown: false}} initialParams={params} />
    </Stack.Navigator>
);

};

And that's it. Then from the screen I got them like this:

const Order = ({route}) => {
  const {itemSelected} = route.params;
  const {first_name, last_name} = itemSelected;
  return (...)
}
like image 21
Gerson Montenegro Avatar answered Oct 01 '22 02:10

Gerson Montenegro