1

I'm using react-navigation v6 in my React Native app, and in this section on passing params to screens, it shows that you can get itemId and otherParam from route.params and set their values such that you can use itemId and otherParam throughout the component.

function DetailsScreen({ route, navigation }) {
  /* 2. Get the param */
  const { itemId, otherParam } = route.params;

What I Want To Know: If I'm using a class component instead of a functional one, where can I set itemId and otherParam? As in, is there a place in a class component where I could run const { itemId, otherParam } = this.props.route.params;?

Drew Reese
  • 165,259
  • 14
  • 153
  • 181
gkeenley
  • 6,088
  • 8
  • 54
  • 129
  • 1
    Possible duplicate https://stackoverflow.com/a/45390937/1667803 – Jorge Pérez May 31 '23 at 05:36
  • Does this answer your question? [How to Pass Parameters to screen in StackNavigator?](https://stackoverflow.com/questions/45388957/how-to-pass-parameters-to-screen-in-stacknavigator) – Drew Reese Jun 07 '23 at 07:57

1 Answers1

0

You can use CommonActions from import and navigation from this.props.navigation https://reactnavigation.org/docs/navigation-actions/#setparams

import { CommonActions } from '@react-navigation/native';

navigation.dispatch({
  ...CommonActions.setParams({ user: 'Wojtek' }),
  source: route.key,
});
Vasyl Nahuliak
  • 1,912
  • 2
  • 14
  • 32