You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native(Expo Go)返回页面时无报错崩溃求助

Expo Go iOS端调用router.back()崩溃问题排查方案

问题背景

使用npx create-expo-app创建项目,基于expo-router开发路由,router.push("profile")跳转正常,但在iOS设备的Expo Go中调用router.back()返回时无报错崩溃,web端功能正常。

排查与解决步骤

  • 更新依赖与Expo Go版本

    1. 确保iOS设备上的Expo Go已更新至App Store最新版本
    2. 检查项目依赖版本,执行命令:
      npm list expo expo-router
      
    3. 若版本非最新,执行更新:
      npx expo install expo@latest expo-router@latest
      
  • 替换router.back()为router.pop()
    expo-router中router.pop()是Stack布局下返回上一页的标准方法,修改profile.tsx中的返回逻辑:

    // profile.tsx
    <Button title="Back" onPress={() => router.pop()} />
    
  • 配置Stack布局的默认头部
    在_layout.tsx中启用默认头部,测试系统自带返回按钮是否崩溃,排除自定义调用问题:

    // _layout.tsx
    import { Stack } from "expo-router";
    
    export default function Layout() {
      return (
        <Stack
          screenOptions={{
            headerShown: true,
          }}
        />
      );
    }
    
  • 清除Expo Go缓存
    在iOS的Expo Go中长按项目图标,选择「Clear Cache」,之后重新加载项目。

  • 检查组件卸载时的副作用清理
    确保页面组件在卸载时已清理所有订阅、定时器等副作用,例如:

    useEffect(() => {
      const interval = setInterval(() => { /* 业务逻辑 */ }, 1000);
      return () => clearInterval(interval); // 必须清理
    }, []);
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 07:35:05