question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to make TapGestureHandler work correctly in functional components?

See original GitHub issue

I have this very simple tap listener for:

  • single tap
  • double tap

written in functional component.

const PressBox = () => {
  const doubleTapRef = React.createRef();

  const _onSingleTap = (event) => {
    if (event.nativeEvent.state === State.ACTIVE) {
      alert("I'm touched");
    }
  };
  const _onDoubleTap = (event) => {
    if (event.nativeEvent.state === State.ACTIVE) {
      alert('D0able tap, good job!');
    }
  };

  return (
    <TapGestureHandler onHandlerStateChange={_onSingleTap} waitFor={doubleTapRef}>
      <TapGestureHandler ref={doubleTapRef} onHandlerStateChange={_onDoubleTap} numberOfTaps={2}>
        <View style={styles.box} />
      </TapGestureHandler>
    </TapGestureHandler>
  );
};

Problem

Double-tap never gets fired. Only single taps

Expected

_onDoubleTap to fire on double tap

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8

github_iconTop GitHub Comments

2reactions
ChronSyncommented, Nov 13, 2020

For anyone who ends up here looking for a solution, React.useRef() hook is needed instead of React.createRef();

import React, { useRef } from "react";
import { TapGestureHandler, State } from "react-native-gesture-handler";
import { View, Text } from "react-native";

export const PressBox = () => {
  const doubleTapRef = useRef(null)
  const _onSingleTap = (event) => {
    if (event.nativeEvent.state === State.ACTIVE) {
      alert("Hey");
    }
  };

  const _onDoubleTap = (event) => {
    if (event.nativeEvent.state === State.ACTIVE) {
      alert("Hey dbl");
    }
  }
  
  return (
    <TapGestureHandler
      onHandlerStateChange={_onSingleTap}
      waitFor={doubleTapRef}>
      <TapGestureHandler ref={doubleTapRef} onHandlerStateChange={_onDoubleTap} numberOfTaps={2}>
        <View style={{ width: 100, height: 100, backgroundColor: "red" }}>
          <Text>HEY!</Text>
        </View>
      </TapGestureHandler>
    </TapGestureHandler>
  );
}
1reaction
timurridjanoviccommented, Jul 17, 2021

Made it work… I had to abstract out all the logic into its own separate component… That made it work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Native Gesture Handler: Swipe, long-press, and more
Implementing gestures in a React Native app improves the user experience. Learn how to create swipeable, pan, long-press, and other ...
Read more >
React Native TapGestureHandler - how to call a function and ...
You can achieve this using a inline callback function and which inturns call ... Also Make sure, You bind this variable properly inside...
Read more >
How to Use Functional Components in React - freeCodeCamp
Have you wondered how to create a component in React? To answer, it is as simple as creating a function returning an HTML-like...
Read more >
React Functional Components: In-Depth Guide - KnowledgeHut
React functional components are just normal JavaScript functions; we can create them using specific function keywords. Most developers create ...
Read more >
Buttons | React Native Gesture Handler - Software Mansion
Gesture handler library provides native components that can act as buttons. ... function that gets triggered when button changes from inactive to active...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found