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

  1. Visit the Snack project: https://snack.expo.dev/@thepoli/2be69c
  2. Choose My Device as the running option.
  3. Scan the displayed QR Code using Expo GO (version 2.32.17).
  4. Test by clicking the buttons — everything works fine at this stage.
  5. Scroll down.
  6. 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.
  7. Remove lines 24, 25, and 26:
<Animated.View style={defaultTitleAnimStyle}>
    <Text>My App Animated Title</Text>
</Animated.View>
  1. 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)

Commits related to this issue

Most upvoted comments

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):

updateRuntimeShadowNodeReferencesOnCommit=true
useShadowNodeStateOnClone=true

If you want to dig more into this feature flags you can go to the related PR.

any workaround? i can not release my update because of this 😦

You can use <Pressable /> from react-native-gesture-handler. BUT there’s another bug where you can’t change color of android_ripple.

https://github.com/software-mansion/react-native-gesture-handler/issues/3246

any workaround? i can not release my update because of this 😦

Heyo,

Had issues as well, I’m using expo 52. What I’ve done is that I’ve upgraded to RN 0.77 and installed these package versions as per expo’s recommendations - npx expo install react-native@~0.77.0 react-native-reanimated@~3.16.7 react-native-gesture-handler@~2.22.0 react-native-screens@~4.5.0 react-native-safe-area-context@~5.1.0 react-native-webview@~13.13.1

Everything is back to normal. https://expo.dev/changelog/2025/01-21-react-native-0.77

I have those packages exactly and issue is only on NewArch , I had to turn off NewArch unfortunately and now everything works.

any workaround? i can not release my update because of this 😦

You can use <Pressable /> from react-native-gesture-handler. BUT there’s another bug where you can’t change color of android_ripple.

software-mansion/react-native-gesture-handler#3246

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