You need to enable JavaScript to run this app. React Native函数组件点击Logout调用handleRequest报undefined错误咨询
优惠活动
大模型
产品
解决方案
定价
更多

React Native函数组件点击Logout调用handleRequest报undefined错误咨询

错误根因
  • HomeScreen是纯函数式组件,不存在类组件实例的this上下文,原代码中this.handleRequest.bind(this)的写法会因为this本身为undefined,直接触发「undefined is not an object」的运行时错误。
  • 原代码中handleRequest定义在组件外部作用域,和this的调用逻辑完全割裂,即便this存在有效上下文,也无法匹配到对应的函数引用。
  • 代码中引用的axios、第三方导航工具Actions没有做导入声明,即便修正了绑定关系,后续运行也会抛出模块未定义的错误。
修正步骤
  1. 移除所有函数组件内的this相关逻辑,函数式组件不需要通过bind绑定上下文,只要函数在可访问的作用域内,直接传入引用即可。
  2. 补全缺失的依赖导入,同时复用现有props传入的navigation对象做路由跳转,不需要额外引入第三方导航工具。
  3. 调整handleRequest的调用逻辑,确保点击按钮时才触发执行。
修正后可直接运行的代码
import { Text, StyleSheet, View, Button } from 'react-native'
import React from 'react'
// 补全axios依赖导入
import axios from 'axios'

// handleRequest定义在组件外部,通过参数接收navigation实例
const handleRequest = (navigation) => {
    axios
        .get('http://192.168.0.213:8000/api/auth/logout/')
        .then(response => {
            console.log(response)
            // 用传入的navigation做路由跳转,替代原代码中的Actions.auth()
            navigation.navigate('Login')
        })
        .catch((err) => {
            console.log(err)
        })
}

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>
            {/* 移除原this绑定逻辑,直接在点击回调中触发handleRequest */}
            <Button 
                title="Logout" 
                onPress={() => handleRequest(navigation)} 
            />
        </View>
    </View>
}

export default HomeScreen
更简洁的可选写法

你也可以把handleRequest定义在组件内部,通过闭包直接获取props里的navigation,不需要额外传参:

const HomeScreen = ({ navigation }) => {
    // 函数定义在组件内部,天然能访问到上层的navigation对象
    const handleRequest = () => {
        axios
            .get('http://192.168.0.213:8000/api/auth/logout/')
            .then(response => {
                console.log(response)
                navigation.navigate('Login')
            })
            .catch((err) => {
                console.log(err)
            })
    }

    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={handleRequest} />
            </View>
        </View>
    )
}
开发注意事项
  • 函数式组件作用域下this默认为undefined,this.xxx属于类组件的专属写法,在函数组件中使用会直接触发运行时错误。
  • onPress属性需要传入点击时执行的函数引用,不要传入函数的立即执行结果(比如onPress={handleRequest()}),否则会在组件渲染阶段就触发接口调用,而非等待用户点击操作。

内容的提问来源于stack exchange,提问作者Trinkflasche93

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.14 17:15:03