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

Expo构建APK中React Native AsyncStorage失效问题求助

问题排查与解决步骤

1. 先确认AsyncStorage的实际存储状态

在登录成功存储Token的代码后,立即添加读取验证逻辑,打包后通过日志确认存储结果:

await AsyncStorage.setItem('token', receivedToken);
const storedToken = await AsyncStorage.getItem('token');
console.log('存储的Token:', storedToken);

打包后可以用adb logcat *:S ReactNative:V ReactNativeJS:V命令查看日志,判断是存储失败还是跳转逻辑的问题。

2. 检查AsyncStorage的导入与依赖

确保使用Expo官方推荐的依赖包,而非已废弃的内置模块:

// 正确导入
import AsyncStorage from '@react-native-async-storage/async-storage';

同时在app.json/app.config.js中添加插件配置:

{
  "expo": {
    "plugins": ["@react-native-async-storage/async-storage"]
  }
}

3. 清理缓存重新构建EAS包

之前的缓存可能导致配置不生效,执行带清理参数的构建命令:

eas build --platform android --clear-cache

4. 排查跳转逻辑的异步顺序问题

确保跳转操作在Token存储完成后执行,避免异步操作未完成就跳转:

// 错误示例:未等待存储完成就跳转
AsyncStorage.setItem('token', token);
navigation.navigate('Home');

// 正确示例:等待存储异步操作完成
await AsyncStorage.setItem('token', token);
navigation.navigate('Home');

5. 调整metro.config.js配置

替换为以下配置,确保打包时模块解析正常:

const { getDefaultConfig } = require('@expo/metro-config');

const config = getDefaultConfig(__dirname);

config.resolver.sourceExts.push('cjs');
config.transformer.inlineRequires = false;

module.exports = config;

内容的提问来源于stack exchange,提问作者Daniel Salazar Rodriguez

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 07:12:04