1

I have a (probably) very simple question:

I am trying to call the function handleRequest as soon as the button Logout is pressed, but I am getting an error that the undefined is not an object. I think, I need to rearrange the functions, but I am not sure how, as I am new to React Native.

Hopefully someone can help me.

All the best Nader

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

const HomeScreen = ({ navigation }) => {

    return <View>
        <Text>Home</Text>
        <Button
            onPress={() => navigation.navigate('Login')}
            title='Login'
        />
        <Text></Text>
        <Button
            onPress={() => navigation.navigate('Register')}
            title='Register'
        />
        <Text></Text>
        <View>
            <Button title="Logout" onPress={this.handleRequest.bind(this)} />
        </View>
    </View>
}



const handleRequest = () => {
    axios
        .get('http://192.168.0.213:8000/api/auth/logout/')
        .then(response => {
            console.log(response)
            Actions.auth()
        })
        .catch((err) => {
            console.log(err)
        })
}

export default HomeScreen
1
  • what is Actions inside then of handleRequest function ? Commented Dec 8, 2020 at 12:03

2 Answers 2

2

call by this :

onPress={()=>handleRequest()}

always remember this or bind are class property

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

Comments

2

change button to

<Button title="Logout" onPress={handleRequest} />

this is not needed in functional components. also consider moving handleRequest function above or inside HomeScreen.

Other possible issues are missing imports to axios and Actions.

Comments

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.