skip to Main Content

I have 2 Screens im trying to pass parameters between. HomeScreen is the previous screen in this case.

function HomeScreen(props: {
  //some props
  route:any, 
  navigation:any,
}) {
   React.useEffect(() => {
     if (props.route.params?.defined) {
       console.log(props.route.params?.defined);
     }
   }, ); //rest of the code

Second component

function SavedRoutesScreen(props: { navigation: any }) {
  function handleGo() {
   props.navigation.navigate({
     name: "Home",
     params: { defined: true },
   });
  } //rest of the code

Basically im trying to pass parameters from SavedRoutesScreen to HomeScreen but I get this error. : "TypeError: undefined is not an object (evaluating ‘props.route.params’)"
anyone know whats up with this?

2

Answers


  1. I think you should use navigation prop object instead of route.

    React.useEffect(() => {
         if (props.navigation.params?.defined) {
           console.log(props.navigation.params?.defined);
         }
       }, ); //rest of the code
    
    

    If that does not work, you should try using .getParams https://www.javatpoint.com/react-native-passing-value-between-screen

    Login or Signup to reply.
  2. You need to pass two arguments to the navigation.navigate() method. I have seen that you are passing an object which is not correct.

    navigation.navigate(name, params)

    name – A destination name of the route that has been defined somewhere

    params – Params to pass to the destination route.

    Try this:

     props.navigation.navigate("Home", { defined: true });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search