Skip to content

onPress is not called after using useNativeDriver transform button #36504

Closed

Description

@ThinkerLu

Description

After using useNativeDriver transform button, finger movement occurs when the button is pressed, the button's onPress will not be called, but the visual feedback of the button is normal.
The bug only occurs on Android devices, and when the new architecture is enabled, useNativeDriver=true is set.

React Native Version

0.71.4

Output of npx react-native info

System:
OS: macOS 13.1
CPU: (10) arm64 Apple M1 Pro
Memory: 148.16 MB / 16.00 GB
Shell: 5.8.1 - /bin/zsh
Binaries:
Node: 19.7.0 - /opt/homebrew/bin/node
Yarn: 1.22.19 - /opt/homebrew/bin/yarn
npm: 9.5.0 - /opt/homebrew/bin/npm
Watchman: 2023.03.06.00 - /opt/homebrew/bin/watchman
Managers:
CocoaPods: Not Found
SDKs:
iOS SDK:
Platforms: DriverKit 22.2, iOS 16.2, macOS 13.1, tvOS 16.1, watchOS 9.1
Android SDK:
API Levels: 28, 29, 30, 31, 32, 33
Build Tools: 28.0.3, 30.0.2, 30.0.3, 31.0.0, 33.0.0
System Images: android-31 | Google APIs ARM 64 v8a, android-33 | Google APIs ARM 64 v8a
Android NDK: Not Found
IDEs:
Android Studio: Electric Eel 2022.1.1 Patch 2 Electric Eel 2022.1.1 Patch 2
Xcode: 14.2/14C18 - /usr/bin/xcodebuild
Languages:
Java: 11.0.15 - /opt/homebrew/opt/openjdk@11/bin/javac
npmPackages:
@react-native-community/cli: Not Found
react: 18.2.0 => 18.2.0
react-native: 0.71.4 => 0.71.4
react-native-macos: Not Found
npmGlobalPackages:
react-native: Not Found

Steps to reproduce

Run my code, Press the button after scrolling the FlatList, move your finger slightly and lift it up.

Snack, code example, screenshot, or link to a repository

import React, {useRef, useState} from 'react';
import {View, Text, Button, Animated} from 'react-native';

const componentList: number[] = Array.from(new Array(100).keys());

const App = () => {
const currScroll = useRef(new Animated.Value(0)).current;
const [count, setCount] = useState(0);

return (
<View style={{flex: 1}}>
<Animated.View
style={{
position: 'absolute',
zIndex: 2,
width: '100%',
transform: [{translateY: currScroll}],
}}>
<Button
title={Press count : ${count}}
onPress={() => {
console.log('pressed');
setCount(count + 1);
}}>
</Animated.View>
<Animated.FlatList
style={{width: '100%', height: '100%', position: 'absolute', zIndex: 1}}
data={componentList}
renderItem={({index}) => (
<Text
style={{
backgroundColor: 'white',
height: 28,
}}>
{index}

)}
keyExtractor={item => item.toString()}
onScroll={Animated.event(
[
{
nativeEvent: {
contentOffset: {
y: currScroll,
},
},
},
],
{useNativeDriver: true},
)}></Animated.FlatList>

);
};

export default App;

Activity

  1. added
    Type: New Architecture
    on Mar 16, 2023
  2. iMonk777 commented on May 5, 2023

    @iMonk777

    Hi @ThinkerLu , we still have this issue. Did you find any solution for this?

  3. jugshaurya commented on May 29, 2023

    @jugshaurya

    +1, after applying transform: translateX/Y/Z(*), on Animated.View, internal content of this View become non-responsive, works after multiple clicking.

    It comes after using New Arch.Even used latest 0.72.0-RC.3

    Tested Today: 29/05/23

  4. skinsapp commented on Jun 11, 2023

    @skinsapp

    @jugshaurya I can confirm I'm having the same issue, using 71.10, android. It basically makes the new architecture unusable on Android, unless you plan to have no animation. It's occurring with all touchables and pressables.

    related: #36710

  5. jugshaurya commented on Jun 11, 2023

    @jugshaurya

    @jugshaurya I can confirm I'm having the same issue, using 71.10, android. It basically makes the new architecture unusable on Android, unless you plan to have no animation. It's occurring with all touchables and pressables.

    related: #36710

    I found a temp/possible solution for it, after researching for more than 2 weeks.

    Use onPressIn instead of onPress !

    Let me know if it also works for you. Also to increase tappable area to make button more responsive, you can use hitslop prop.

  6. vrgimael commented on Jun 28, 2023

    @vrgimael

    @jugshaurya I can confirm I'm having the same issue, using 71.10, android. It basically makes the new architecture unusable on Android, unless you plan to have no animation. It's occurring with all touchables and pressables.
    related: #36710

    I found a temp/possible solution for it, after researching for more than 2 weeks.

    Use onPressIn instead of onPress !

    Let me know if it also works for you. Also to increase tappable area to make button more responsive, you can use hitslop prop.

    This is not a solution as it triggers accidentally on any scrollable item

  7. jugshaurya commented on Jun 28, 2023

    @jugshaurya

    @jugshaurya I can confirm I'm having the same issue, using 71.10, android. It basically makes the new architecture unusable on Android, unless you plan to have no animation. It's occurring with all touchables and pressables.
    related: #36710

    I found a temp/possible solution for it, after researching for more than 2 weeks.

    Use onPressIn instead of onPress !

    Let me know if it also works for you. Also to increase tappable area to make button more responsive, you can use hitslop prop.

    This is not a solution as it triggers accidentally on any scrollable item

    Can you share an example gif or something to get the better context, because i used this solution for codebase, it is working fine.

  8. github-actions commented on Dec 26, 2023

    @github-actions

    This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

  9. added
    Stale
    on Dec 26, 2023
  10. github-actions commented on Jan 2, 2024

    @github-actions

    This issue was closed because it has been stalled for 7 days with no activity.

  11. removed
    Stale
    on Jan 7, 2024
  12. 14 remaining items

  13. fdecampredon commented on Oct 29, 2024

    @fdecampredon

    @61xin After upgrading to 0.76.1 I can say that the issue is still present

  14. cipolleschi commented on Oct 31, 2024

    @cipolleschi
    Contributor

    @fdecampredon can you share a reproducer? We have a screen in RNTester to test this case and I'm sure it was fixed. Maybe we missed some edge case.

  15. fdecampredon commented on Nov 22, 2024

    @fdecampredon

    @cipolleschi I'm really sorry after more investigation I think it is more related to this one : #44768.
    We'll try to make a minimal reproduction example but it's a bit hard since the problem is deeply nested in a very complex screen.

  16. ZhangBinglei2015 commented on Dec 12, 2024

    @ZhangBinglei2015

    I am using react native 0.72 and this solve my problem,you can try it!

    import type {ViewProps} from 'react-native'
    import {PanResponder, View} from 'react-native'
    
    export type TouchComponentProps = {
      onPress: () => void
    }
    
    export default (props: ViewProps & TouchComponentProps) => {
      const panresponder = PanResponder.create({
        onStartShouldSetPanResponder: () => true,
        onShouldBlockNativeResponder: () => false,
        onPanResponderRelease: () => {
          props.onPress()
        },
      })
    
      return (
        <View {...props} {...panresponder.panHandlers}>
          {props.children}
        </View>
      )
    }
    
  17. kirillzyusko commented on May 21, 2025

    @kirillzyusko
    Contributor

    @cipolleschi I believe I encountered a very similar issue and thought to ask your help 😅

    Basically I have this component:

    const KeyboardStickyView = ({ children }) => {
      const { height: progress } = useKeyboardAnimation();
    
      return (
        <Animated.View
          style={{
            transform: [{ translateY: Animated.multiply(progress, 0.3) }],
          }}
        >
          {children}
        </Animated.View>
      );
    };

    Where useKeyboardAnimation simply subscribes to the context (that stores animated value):

    import React, { useMemo } from "react";
    import { Animated, Platform, StyleSheet, View } from "react-native";
    
    import { KeyboardControllerView } from "./bindings";
    import { KeyboardContext } from "./context";
    import { useAnimatedValue } from "./internal";
    
    import type { KeyboardAnimationContext } from "./context";
    import type { ViewStyle } from "react-native";
    
    const IS_EDGE_TO_EDGE = true;
    
    const KeyboardControllerViewAnimated = Animated.createAnimatedComponent(
      KeyboardControllerView,
    );
    
    type Styles = {
      container: ViewStyle;
    };
    
    const styles = StyleSheet.create<Styles>({
      container: {
        flex: 1,
      },
    });
    
    type KeyboardProviderProps = {
      children: React.ReactNode;
      statusBarTranslucent?: boolean;
      navigationBarTranslucent?: boolean;
      preserveEdgeToEdge?: boolean;
      enabled?: boolean;
    };
    
    export const KeyboardProvider = (props: KeyboardProviderProps) => {
      const {
        children,
        statusBarTranslucent,
        navigationBarTranslucent,
        preserveEdgeToEdge,
      } = props;
      // animated values
      const progress = useAnimatedValue(0);
      const height = useAnimatedValue(0);
      // memo
      const context = useMemo<KeyboardAnimationContext>(
        () => ({
          animated: { progress: progress, height: Animated.multiply(height, -1) },
        }),
        [],
      );
      const onScroll = useMemo(
        () =>
          Animated.event(
            [
              {
                nativeEvent: {
                  contentOffset: { y: height, x: progress },
                },
              },
            ],
            { useNativeDriver: true },
          ),
        [],
      );
      const onKeyboardMove = useMemo(
        () =>
          Animated.event(
            [
              {
                nativeEvent: {
                  progress: progress,
                  height: height,
                },
              },
            ],
            { useNativeDriver: true },
          ),
        [],
      );
    
      return (
        <KeyboardContext.Provider value={context}>
          <Animated.ScrollView style={{ maxHeight: 200 }} onScroll={onScroll}>
            <View style={{ height: 600, backgroundColor: "black" }} />
          </Animated.ScrollView>
          <KeyboardControllerViewAnimated
            enabled={true}
            navigationBarTranslucent={IS_EDGE_TO_EDGE || navigationBarTranslucent}
            statusBarTranslucent={IS_EDGE_TO_EDGE || statusBarTranslucent}
            preserveEdgeToEdge={IS_EDGE_TO_EDGE || preserveEdgeToEdge}
            style={styles.container}
            // map to Animated value
            onKeyboardMove={onKeyboardMove}
          >
            {children}
          </KeyboardControllerViewAnimated>
        </KeyboardContext.Provider>
      );
    };

    And here I have few strange observations:

    • the issue is reproducible only on physical Android device (on emulator it's not reproducible);
    • when I update progress/height values through onKeyboardMove - the issue becomes reproducible.
    • when the problem became reproducible and if I slightly scroll a ScrollView (that also updates height/progress), then onPress handler works well again;
    • if I add a code that will re-render KeyboardStickyView then the issue will gone too.

    It seems like I encounter exactly the problem described in #45413 But what I don't understand is why events sent through ScrollView works as expected, but events sent from KeyboardControllerView somehow updates an animated value, but don't "commit" final position, so that touches gets rejected?

    I would appreciate any help/response from you 🙌

  18. kirillzyusko commented on May 21, 2025

    @kirillzyusko
    Contributor

    Okay, after a little bit debugging I found out, that one scroll gets performed all Animated.View gets re-rendered:

    Image

    Including my view

    Image

    And it actually explains a lot. If I understand correctly that re-render synchronizes C++ state with native-driver animations state, so that all Touchables are aware about layout shift and can correctly fire onPress 🤔

    The open question now is how onScrollEnd event notifies all animated views to re-render them 👀

  19. kirillzyusko commented on May 21, 2025

    @kirillzyusko
    Contributor

    After digging in the code even more I realized that for a ScrollView you additionally call userDrivenScrollEnded method, which sends onUserDrivenAnimationEnded event, and this event gets broadcasted to all Animated.Views:

    NativeAnimatedHelper.nativeEventEmitter.addListener(
      'onUserDrivenAnimationEnded',
      data => {
        node.update();
      },
    );

    While I understand, that this approach works for ScrollView... Is this approach scalable? For example I may want to send onUserDrivenAnimationEnded when keyboard finishes its animation to synchronize shadow-tree, but keyboard animation is not even user-driven animation 🙈

    And theoretically for each 3rd party package that drives animations (i. e. changes translate via native driver) we have to add this "synchronization" code?

  20. cipolleschi commented on Jun 9, 2025

    @cipolleschi
    Contributor

    @kirillzyusko sorry for the delay... You performed a very deep investigation, so you have discovered almost everything yourself! :D

    The problem here is how to keep the react tree in sync with the shadow tree and that in sync with the native tree.

    The approach of onUserDrivenAnimationEnded works with ScrollView and every component based on ScrollView gets it for free.
    This works for animation driven by the user, because we don't know when the animation actually ends.
    For timed animations, such as the opening/closing of the keyboard, we know exactly when the Animation end. We also usually have a callback for it. In those cases, there is another events that the component should emit, that forces the alignment of the trees: i think that is onAnimatedValueUpdate, but I might have to double check.

    It should be the same event emitted by scroll view when you call scrollTo.

    When we have natively driven animations, sending an event from native to JS is the only way to tell React that the trees need to be realigned.

    It scales, as an approach, but we would have to be clearer with our Documentation, and given that the code changes so fast, we haven't been as good as we should with that.

    We are also exploring other alternatives to the Animated backend, so I hope that the situation could improve soon!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Component: ButtonResolution: FixedType: New Architecture

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions