react native navigation pass props code example

Example 1: react native passing parameters to routes

Copy
function HomeScreen({ navigation }) {
  return (
    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      <Text>Home Screen</Text>
      <Button
        title="Go to Details"
        onPress={() => {
          /* 1. Navigate to the Details route with params */
          navigation.navigate('Details', {
            itemId: 86,
            otherParam: 'anything you want here',
          });
        }}
      />
    </View>
  );
}

function DetailsScreen({ route, navigation }) {
  /* 2. Get the param */
  const { itemId } = route.params;
  const { otherParam } = route.params;
  return (
    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      <Text>Details Screen</Text>
      <Text>itemId: {JSON.stringify(itemId)}</Text>
      <Text>otherParam: {JSON.stringify(otherParam)}</Text>
      <Button
        title="Go to Details... again"
        onPress={() =>
          navigation.push('Details', {
            itemId: Math.floor(Math.random() * 100),
          })
        }
      />
      <Button title="Go to Home" onPress={() => navigation.navigate('Home')} />
      <Button title="Go back" onPress={() => navigation.goBack()} />
    </View>
  );
}

Example 2: navigation react pass props

Copyfunction HomeScreen({ navigation }) {  return (    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>      <Text>Home Screen</Text>      <Button        title="Go to Details"        onPress={() => {          /* 1. Navigate to the Details route with params */          navigation.navigate('Details', {            itemId: 86,            otherParam: 'anything you want here',          });        }}      />    </View>  );}
function DetailsScreen({ route, navigation }) {  /* 2. Get the param */  const { itemId } = route.params;  const { otherParam } = route.params;  return (    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>      <Text>Details Screen</Text>      <Text>itemId: {JSON.stringify(itemId)}</Text>      <Text>otherParam: {JSON.stringify(otherParam)}</Text>      <Button        title="Go to Details... again"        onPress={() =>          navigation.push('Details', {            itemId: Math.floor(Math.random() * 100),          })        }      />      <Button title="Go to Home" onPress={() => navigation.navigate('Home')} />      <Button title="Go back" onPress={() => navigation.goBack()} />    </View>  );}