Expo SDK49升级后react-native-reanimated v3结合SafeAreaView首次动画卡顿
React Native Reanimated 3.3.0 首次动画卡顿(关联SafeAreaView)修复方案
给你几个实用的修复思路:
替换原生SafeAreaView为Expo官方实现
先安装依赖(如果没装过):npx expo install expo-safe-area-context然后把代码里
import { SafeAreaView } from 'react-native'替换成:import { SafeAreaView } from 'expo-safe-area-context';用这个版本的SafeAreaView包裹页面内容,它和Reanimated 3.x的兼容性更好,不会触发首次动画卡顿的问题。
延迟首次动画执行
给首次动画加个短暂延迟,等Reanimated和SafeAreaView都初始化完毕再触发,示例代码:import { useEffect } from 'react'; import { useSharedValue, withTiming } from 'react-native-reanimated'; function YourAnimatedComponent() { const animValue = useSharedValue(0); useEffect(() => { const timer = setTimeout(() => { animValue.value = withTiming(1, { duration: 500 }); }, 100); return () => clearTimeout(timer); }, []); // 这里写你的动画视图逻辑 }临时降级Reanimated版本
如果上面的方法都不生效,可以先降级回之前的稳定版本2.14.4,保证业务正常运行,等Reanimated后续版本修复这个兼容性问题:npm install react-native-reanimated@2.14.4 # 用yarn的话就执行 yarn add react-native-reanimated@2.14.4降级后记得重新执行
npx expo prebuild生成原生文件,再重新构建dev client。提前初始化Reanimated
在App启动时手动触发Reanimated的初始化,避免首次动画时才开始初始化导致卡顿:import { useEffect } from 'react'; import { initializeReanimated } from 'react-native-reanimated'; export default function App() { useEffect(() => { initializeReanimated(); }, []); return ( {/* 你的App页面内容 */} ); }
内容的提问来源于stack exchange,提问作者Niccolò Caselli
相关产品推荐
相关产品推荐

