React Native中用aws-amplify实现登录遇Logger构造函数错误求助
解决React Native中aws-amplify登录功能的
Logger is not a constructor错误 错误原因
这个错误源于aws-amplify v6的API重大重构和UI库版本不兼容:
- aws-amplify v6彻底移除了
Logger构造函数,改用全新的日志实现逻辑; - 你安装的
aws-amplify-react-nativev6是官方废弃的旧包,内部仍在调用v5版本的LoggerAPI,与v6核心库产生冲突; - 官方在v6版本后已将React Native的UI组件迁移至
@aws-amplify/ui-react-native,旧包不再维护更新。
解决方案
1. 卸载废弃的UI包
执行命令移除旧的aws-amplify-react-native:
npm uninstall aws-amplify-react-native # 若使用yarn则执行: yarn remove aws-amplify-react-native
2. 修改App.js代码,使用官方新UI库
替换withAuthenticator的导入源,并确保正确包裹根组件:
import { StatusBar } from 'expo-status-bar'; import React from 'react'; import { StyleSheet, Text, View } from 'react-native'; import { Amplify } from 'aws-amplify'; // 从官方新UI库导入withAuthenticator import { withAuthenticator } from '@aws-amplify/ui-react-native'; import { config } from './aws-exports'; Amplify.configure(config); function App() { return ( <View style={styles.container}> <Text>Open up App.js to start working on your app!</Text> <StatusBar style="auto" /> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center', }, }); // 用withAuthenticator包裹根组件 export default withAuthenticator(App);
3. 清理缓存并重启项目(可选但推荐)
避免缓存残留导致的异常,执行以下操作:
# 删除依赖包和锁文件 rm -rf node_modules package-lock.json yarn.lock # 重新安装依赖 npm install # 或 yarn install # 清除Expo缓存并重启开发服务器 expo start --clear
验证
重新启动项目后,登录界面应能正常加载,Logger is not a constructor的错误将不再出现。
内容的提问来源于stack exchange,提问作者Shauij Msw
相关产品推荐
相关产品推荐

