0

I need to navigate to m home screen when I press a button. But I get an Error => TypeError: undefined is not an object (evaluating 'navigation.navigate')

My Code

import React, { Component, useEffect  } from 'react'    
import { Card } from 'react-native-elements'         
import AsyncStorage from '@react-native-async-storage/async-storage';
import screenStyles from './ScreenStyles';
import HomeHeader from '../components/HomeHeader';
import TicketCardList from '../components/TicketCardList';
import DisableModal from '../components/disableModal';
import { Camera } from 'expo-camera';
import * as Permissions from 'expo-permissions';
import {API_URL, API_TOKEN, API_URL2} from "@env"
import ClearLocalStorage from '../config/clearLocalStorage';

export default class CreateTroubleT extends Component {

    constructor(){
        super();
    }

    
    cancelButtonPress =() => {
        this.props.navigation.navigate('HomeBottomBar');
        console.log("OK")
    }


    render() {

        return (                            
                            
                        <TouchableOpacity style={styles.cancelJBDesing} onPress={()=> this.cancelButtonPress()}>
                        </TouchableOpacity>  
        )
    }
}

When I press the Cancel button I get the Error. I'm new to Native so appreciate any suggestions

2 Answers 2

2

The problem is because when you pressed the button, you said you want to invoke cancelButtonPress with no parameter as shown below

onPress={()=> this.cancelButtonPress()}

But when you defined cancelButtonPress, it is a function that expect a parameter as shown below

cancelButtonPress (navigation) {
    navigation.navigate('HomeBottomBar')
    console.log("OK")
}

Since when you invoked cancelButtonPress without passing parameter, navigation is undefined, and which is why navigation.navigate throwing you that error.

solution:

Actually you should not be passing parameter when you pressed cancelButtonPress, because navigation is not something that you should pass via function call. Instead, you should update your cancelButtonPress function as below

cancelButtonPress = () => {
  this.props.navigation.navigate('HomeBottomBar');
  console.log("ok");
}

Assuming that you've set up react-navigation correctly, it should be accessible via this.props.navigation instead

Sign up to request clarification or add additional context in comments.

7 Comments

can you please let me know how to set up react-navigation
That is whole lot of works to do, if you tried my solution and it doesn't work, chances are your react-navigation is not being setup properly, and that definitely worth another question solely on it's own
@RyanFonseka: after updated your code what is the error you're facing?
console.log is working but not the this.props.navigation.navigate('HomeBottomBar');
yup, which means like what I've said, you did not setup your react-navigation properly and that is a separate question on it's own. In that new question, what you can do is to share all the code you've setup related to react-navigation and hopefully someone will help you for that
|
0

It appears that you're not providing any argument to the function (hence the value of navigation is indeed undefined). Why don't provide an event e to the cancelButtonPress function an try to console.log the value of navigation?

You'll end up with something like:

<TouchableOpacity style={styles.cancelJBDesing} onPress={e=> this.cancelButtonPress(e)}> (When it comes to your JSX).

cancelButtonPress (navigation) {
    console.log(navigation);
    navigation.navigate('HomeBottomBar');
    console.log("OK");
}

(When it comes to the function).

Such a thing will allow you to have a navigation object which value is different than undefined.

If the you don't want to over complicate yourself, you should just refactor your cancelButtonPress, in order for it to properly implement navigation.navigate.

You'll end up with something like this:

cancelButtonPress () {
   console.log("OK");
   this.props.navigation.navigate("/pathToHome");
}

In this case, you don't need to capture the e events as you don't really need to provide any arguments to your function to get the expected behaviour.

2 Comments

OP is working on a React Native project, so there is no browserHistory in the picture and event in the picture as it's not a web project
onPress={e=> this.cancelButtonPress(e)}> is wrong, there is no such e in TouchableOpacity

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.