You need to enable JavaScript to run this app. React Native Navigation调用navigate报undefined的问题排查
优惠活动
大模型
产品
解决方案
定价
更多

React Native Navigation调用navigate报undefined的问题排查

核心报错原因定位
  • 组件类型和写法完全错位:所有页面都是函数式组件,但代码全程照搬类组件的this语法,没有对应的this实例,所有通过this取到的值都是undefined
    • 写在组件外部的handleSubmit不仅拿不到函数作用域里的navigation对象,定义时写的({navigation})入参也没有任何地方会给它传值
    • 绑定到Button上的onPress={this.handleSubmit}因为this是undefined,调用的根本不是你写的提交逻辑
  • 无效IMEI不崩溃的原因很简单:那个分支只触发Toast提示,完全没有用到navigation对象;只有有效分支走到navigation.navigate时,因为拿不到有效的navigation引用,才会抛出你看到的类型错误
  • 额外的语法隐患:定义Route页面的Drawer.Screen标签漏写了闭合的/>,JSX编译时就会报错,后续也会影响导航功能的正常运行
具体修复方案
  1. 移除函数式组件里所有无效的this写法,把表单状态、提交逻辑全部挪到FeedScreen的函数作用域内部,通过闭包直接获取解构出来的navigation对象,不需要额外传参
  2. 修正TextInput的双向绑定逻辑,原来只写了Name属性,根本没有把输入值和组件状态关联,原来的IMEI取值永远是空的
  3. 清理fetch链式调用里无效的this.setState语句,把状态更新逻辑替换成你实际需要的业务逻辑
修正后的可运行代码
// 修正原Drawer导航的语法漏写问题
const MyDrawer = () => {
  const [show, setShow] = useState(true);
  return (
    <Drawer.Navigator drawerContent={props => <CustomDrawerContent setShow={setShow} {...props} />}>
      <Drawer.Screen name="Feed" component={FeedScreen} />
      {/* 原代码漏写了闭合标签 */}
      <Drawer.Screen name="Route" component={RouteScreen} />
    </Drawer.Navigator>
  );
};

// 修正后的FeedScreen
const FeedScreen = ({ navigation }) => {
  // 用useState管理IMEI输入值,替代原来的this.state
  const [imei, setImei] = useState('');

  // 提交逻辑写在组件作用域内,直接闭包获取navigation
  const handleSubmit = () => {
    const details = {
      imei: imei,
      password: 'Password!',
      action: 'login'
    };
    let formBody = [];
    for (let property in details) {
      const encodedKey = encodeURIComponent(property);
      const encodedValue = encodeURIComponent(details[property]);
      formBody.push(`${encodedKey}=${encodedValue}`);
    }
    formBody = formBody.join('&');

    fetch('http://example.com/api.php', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
        'Accept': 'application/json',
      },
      body: formBody
    })
      .then(res => res.json())
      .then(json => {
        if (json.res === '-1') {
          ToastAndroid.show('device not found', 1000);
          return;
        }
        // 调用闭包内的navigation,不存在undefined问题
        navigation.navigate('Route');
      })
      .catch(error => console.error('error:', error));
  };

  return (
    <View style={styles.container}>
      <View style={styles.containerTop}>
        <Header
          leftComponent={{
            icon: 'menu',
            color: '#fff',
            onPress: () => navigation.openDrawer()
          }}
          centerComponent={{ text: 'device', style: { color: '#fff' } }}
          rightComponent={{ icon: 'home', color: '#fff' }}
        />
      </View>
      <View style={styles.container2}>
        <Text>submit form</Text>
        {/* 修正TextInput的绑定逻辑 */}
        <TextInput
          style={{ height: 40 }}
          placeholder="8604893512478963"
          value={imei}
          onChangeText={setImei}
        />
        {/* 移除this绑定,直接引用作用域内的handleSubmit */}
        <Button title="     login     " onPress={handleSubmit} />
      </View>
    </View>
  );
};

const RouteScreen = ({ navigation }) => {
  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <Text>Route Screen</Text>
    </View>
  );
};
修复后效果验证
  • 输入无效IMEI时,正常弹出Toast提示,不会触发跳转
  • 输入有效IMEI时,接口请求成功后,直接通过Drawer导航栈跳转到Route页面,不会再抛出navigation undefined的错误

内容的提问来源于stack exchange,提问作者peiman F.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.21 09:24:03