React Native 0.73.1中Alert.alert在Android端引发应用异常刷新求助
React Native 0.73.1 Android端Alert.alert触发后页面意外刷新跳转问题解决
检查Alert调用时机
别在组件渲染阶段(比如render函数、依赖不正确的useEffect里)调用Alert.alert(),一定要放在按钮onPress这类事件处理函数中执行。如果在渲染时触发弹窗,Android端很容易出现UI状态紊乱的情况。换用第三方弹窗库替代官方Alert
RN官方Alert在部分Android环境下存在兼容性问题,试试react-native-modal或者react-native-paper里的弹窗组件,这类库的实现更稳定,不会触发页面刷新。举个react-native-modal的使用示例:import Modal from 'react-native-modal'; import { View, Text, Button } from 'react-native'; import { useState } from 'react'; // 组件内部逻辑 const [showAlert, setShowAlert] = useState(false); const triggerAlert = () => { setShowAlert(true); }; return ( <> {/* 你的页面内容 */} <Modal isVisible={showAlert}> <View style={{ backgroundColor: '#fff', padding: 20, borderRadius: 8 }}> <Text style={{ fontSize: 18, fontWeight: '600', marginBottom: 12 }}>Incomplete</Text> <Text style={{ marginBottom: 20 }}>Please make sure all required fields are filled before signing.</Text> <Button title="OK" onPress={() => setShowAlert(false)} /> </View> </Modal> </> );检查AndroidManifest的Activity启动模式
打开android/app/src/main/AndroidManifest.xml,查看主Activity的android:launchMode配置,如果设成了singleTask或singleInstance,改成默认的standard即可,这类特殊启动模式容易导致页面栈异常:<activity android:name=".MainActivity" android:launchMode="standard" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode" android:exported="true" android:windowSoftInputMode="adjustResize">升级RN到同分支最新补丁版
RN 0.73.1可能存在Alert相关的已知bug,升级到0.73.x的最新版本(比如0.73.6),小版本补丁通常会修复这类平台特定的兼容性问题。
内容的提问来源于stack exchange,提问作者Bruno Pintos
相关产品推荐
相关产品推荐

