Search code examples
react-nativereact-navigationtouchableopacitytouchablewithoutfeedback

react-native TouchableNativeFeedback onPress not working


I have created a composed component to compose TouchableNativeFeedback to wrapperComponent.

export default function withFeedback2(
    WrappedComponent
) {
    return class extends BaseComponent {
        constructor(props) {
            super(props);
        }

        render() {
            return (
                <View>
                    <TouchableNativeFeedback
                        onPress={() => this.props.onContainerViewPress()}

                    >
                        <WrappedComponent {...this.props} />
                    </TouchableNativeFeedback>
                    {/* <TouchableOpacity
                        onPress={this.props.onContainerViewPress ? () => this.props.onContainerViewPress() : null}

 >
                        <WrappedComponent {...this.props} />
                    </TouchableOpacity> */}
                </View>
            );
        }
    };
}

But OnPress event of TochableNativeFeedback is not firing. Whereas OnPress event is fired correctly and onContainerViewPress prop of wrappercomponent is called if wrappercomponent wrapped under TouchableOpacity.

I am testing this on the Android Platform.


Solution

  • I've discovered that adding a Ripple effect to the TouchableNativeFeedback fixes the issue for me:

    background={TouchableNativeFeedback.Ripple("#FFFFFF",true)}
    

    i.e.

    export default function withFeedback2(
      WrappedComponent
    ) {
      return class extends BaseComponent {
        constructor(props) {
          super(props);
        }
    
        render() {
          return (
            <View>
              <TouchableNativeFeedback
                onPress={() => this.props.onContainerViewPress()}
                background={TouchableNativeFeedback.Ripple("#FFFFFF",true)}
              >
                <WrappedComponent {...this.props} />
              </TouchableNativeFeedback>
            </View>
          );
        }
      };
    }