react-native-reanimated: not working inside if used anywhere
Description
Hi, I was trying to create an animated header using <Animated.View /> while scrolling, but unfortunately, when I scroll down, my buttons stop working if I include <Animated.View /> with useAnimatedStyle anywhere in the code. This issue only occurs on a real device. However, if I remove style={defaultTitleAnimStyle}, everything works fine.
I also tested this with Pressable from react-native-gesture-handler, and it worked as expected. I’m not sure why this is happening—maybe I’m doing something wrong?
Here are some examples: With <Animated.View style={defaultTitleAnimStyle} />: https://github.com/user-attachments/assets/53ab00af-72a7-4c46-82ee-12486e0bdbab
Without <Animated.View />: https://github.com/user-attachments/assets/34e29e4f-f700-4528-b6b8-295440a671e7
Code is available in Expo Snack and also included below:
import { Pressable, ScrollView, Text, View } from "react-native";
import Animated, { interpolate, useAnimatedStyle, useSharedValue } from "react-native-reanimated";
export default function Index() {
const [pressed, setPressed] = useState(0)
const scrollValue = useSharedValue(0)
const onScroll = useCallback((e: any) => {
scrollValue.value = e.nativeEvent.contentOffset.y
}, [])
const defaultTitleAnimStyle = useAnimatedStyle(() => {
const opacity = interpolate(scrollValue.value, [32, 96], [0, 1])
return {
opacity: opacity,
}
})
return (
<View style={{ flex: 1 }} >
<Animated.View style={defaultTitleAnimStyle}>
<Text>My App Animated Title</Text>
</Animated.View>
<Text>Pressed times: {pressed}</Text>
<Animated.ScrollView onScroll={onScroll}>
<Pressable onPress={() => setPressed(prev => prev + 1)} android_ripple={{ color: 'red' }} style={{ padding: 24, backgroundColor: '#222' }}>
<Text style={{ color: "#fff" }}>My Test Button</Text>
</Pressable>
<Pressable onPress={() => setPressed(prev => prev + 1)} android_ripple={{ color: 'red' }} style={{ padding: 24, backgroundColor: '#222' }}>
<Text style={{ color: "#fff" }}>My Test Button</Text>
</Pressable>
<Pressable onPress={() => setPressed(prev => prev + 1)} android_ripple={{ color: 'red' }} style={{ padding: 24, backgroundColor: '#222' }}>
<Text style={{ color: "#fff" }}>My Test Button</Text>
</Pressable>
<Pressable onPress={() => setPressed(prev => prev + 1)} android_ripple={{ color: 'red' }} style={{ padding: 24, backgroundColor: '#222' }}>
<Text style={{ color: "#fff" }}>My Test Button</Text>
</Pressable>
<Pressable onPress={() => setPressed(prev => prev + 1)} android_ripple={{ color: 'red' }} style={{ padding: 24, backgroundColor: '#222' }}>
<Text style={{ color: "#fff" }}>My Test Button</Text>
</Pressable>
<View style={{ height: 1200 }} />
</Animated.ScrollView>
</View>
);
}
Steps to reproduce
- Visit the Snack project: https://snack.expo.dev/@thepoli/2be69c
- Choose My Device as the running option.
- Scan the displayed QR Code using Expo GO (version 2.32.17).
- Test by clicking the buttons — everything works fine at this stage.
- Scroll down.
- Try clicking the buttons again — you’ll notice that <Pressable /> loses focus. On Android, the ripple effect is visible, but the onPress event doesn’t fire.
- Remove lines 24, 25, and 26:
<Animated.View style={defaultTitleAnimStyle}>
<Text>My App Animated Title</Text>
</Animated.View>
- After removing those lines, everything works correctly again.
Snack or a link to a repository
https://snack.expo.dev/@thepoli/2be69c
Reanimated version
~3.16.1
React Native version
0.76.6
Platforms
Android
JavaScript runtime
None
Workflow
Expo Go
Architecture
Fabric (New Architecture)
Build type
None
Device
Real device
Device model
Samsung Galaxy A32
Acknowledgements
Yes
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 11
- Comments: 23 (7 by maintainers)
Hi there! After some digging, we discovered that the issue is from React Native itself. It appears to be caused by outdated state being cloned, which results in an incorrect hitbox being applied to Pressable components - you can read more about it in this issue (big kudos to @bartlomiejbloniarz ).
So, to fix that you can apply following feature flags to your project (
/android/gradle.properties):If you want to dig more into this feature flags you can go to the related PR.
You can use
<Pressable />fromreact-native-gesture-handler. BUT there’s another bug where you can’t change color ofandroid_ripple.https://github.com/software-mansion/react-native-gesture-handler/issues/3246
any workaround? i can not release my update because of this 😦
I have those packages exactly and issue is only on NewArch , I had to turn off NewArch unfortunately and now everything works.
Unfortunately, my app depend on many libraries. I can not change all of them 😕. TouchableRipple from react-native-paper for example, which uses Pressable and android_ripple
Hi @th3poli, thanks for opening this issue. I think it is directly related to other issue #6704 (and probably #6676) - we’re working to find fix for the issue, and will let you know as soon as it’s ready c:
Yes, that’s right, you’ll need to apply these changes to react-native as well.
To let you know @patrycjakalinska, I’ve tried to test again with TouchableOpacity on the latest nightly build and it didn’t work.
"react-native-reanimated": "4.0.0-nightly-20250318-d13d2bb26"+1