Search code examples
react-nativeparametersreact-propsreact-navigation-v5

How to get a param of an id using react-navigation v5?


I'm trying to update an app I've developed using react-navigation v4.

Using react-navigation v4 I could get the id using something like console.log(navigation.getParam('id'));

But when I tried the same after updating to v5 of react-navigation it shows me an error and I can't find the right way to get the param id

import React from 'react';
import { View, Text, StyleSheet } from 'react-native';

export default function ShowScreen({ navigation }) {
  console.log(navigation.getParam('id'));

  return (
    <View style={styles.container}>
      <Text>Show Screen</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center'
  }
});

The other screen where the id is located is it:

import React, { useContext } from 'react';
import { View, Text, StyleSheet, Button } from 'react-native';
import { FlatList, TouchableOpacity } from 'react-native-gesture-handler';

import { Context } from '../../context/BlogContext';

import Icon from 'react-native-vector-icons/Feather'

export default function IndexScreen({ navigation }) {
  const { state, addBlogPost, deleteBlogPost } = useContext(Context);

  return (
    <View style={styles.container}>
      <Button
        title="Add Post"
        onPress={addBlogPost}
      />
      <FlatList
        data={state}
        keyExtractor={(blogPost) => blogPost.title}
        renderItem={({ item }) => {
          return (
            <TouchableOpacity onPress={
              () => navigation.navigate('ShowScreen',
                { id: item.id })}>
              <View style={styles.row}>
                <Text style={styles.title}>
                  {item.title} -
                  {item.id}
                </Text>
                <TouchableOpacity onPress={() => deleteBlogPost(item.id)}>
                  <Icon style={styles.icon}
                    name="trash"
                  />
                </TouchableOpacity>
              </View>
            </TouchableOpacity>
          );
        }}
      />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    marginTop: 30,
    justifyContent: 'center',
    alignItems: 'center'
  },
  row: {
    flexDirection: 'row',
    justifyContent: 'space-between',
    paddingHorizontal: 10,
    paddingVertical: 20,
    borderTopWidth: 1,
    borderColor: '#333'
  },
  title: {
    fontSize: 18
  },
  icon: {
    fontSize: 24
  }
});

Solution

  • You can get params using route.params in react navigation v5 more on that read here

    just update your code to this

    import React from 'react';
    import { View, Text, StyleSheet } from 'react-native';
    
    export default function ShowScreen({ navigation,route }) {
      const {id} =route.params; //you will get id here via route
    
      return (
        <View style={styles.container}>
          <Text>Show Screen</Text>
        </View>
      );
    }
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center'
      }
    });