Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Implement a Long Press Button with React Native's Gesture Responder System

    react-nativeReact Native

    In this video you'll learn to leverage React Native's Gesture Responder System to create a component that calls a function only if you interact with the component for a set period of time.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    Instructor: I have a React native application with a red button, where when we tap it once, it says, "Box tapped." If we double-tap it, we get double-tapped alerted. To double-tap, you can just press option on your keyboard, within the iOS simulator, to get it to simulate a double-tap.

    Let's refactor this multi-tap component to actually allow us to add a delay. If we double-tap and hold, that's the only point that it's going to take an action versus just an immediate touch and release. You need to hold the app for 1,000 milliseconds, or 1 second.

    Let's go ahead and add a delay prop to our component. We're just going to call this 1,000. I'm also going to add a variable. We're going to call this startPress. We're just going to default this to null.

    Basically, the point at which we want to start detecting the press is when we have established that we should be the responder to this event, to this double-tap event. That's going to happen here when our touches.length is equal to the number of touches that we want to actually detect at this point in time.

    If this is true, if we are actually responding to this event, we can go ahead and say, "this.startPress = Date.now()." Looking at the onResponderRelease, since we're only going to want this to happen when the application has been, by default, held down or the button has been held down for one second, we can go ahead and make a comparison here.

    We'll just grab the time right now. We can say, "now = Date.now()." We'll then go ahead and check that this.startPress actually exists because it defaults to null. If somehow onResponderRelease is called before it exists...We'll just go ahead and make sure that it does indeed exist.

    We'll then go ahead and subtract this.startPress from now, which will give us the millisecond difference between those two times. Then we want to make sure that that is actually greater than our delay.

    If all of these factors are true, we'll go ahead call this.props.onPress. Delay is actually coming from this.props. Now when we go ahead and test this, we can see single-tap press is fine. A quick double-tap, it doesn't do anything. If we double-tap it, hold it, and then release, we can see we get that double-tap.

    The final thing we'll want to do before finishing off this component is just making sure that onResponderRelease, we go ahead and reset this.startPress and set it to the default value of null. Now we've got a multi-tap component that we can go ahead and detect double-taps with a delay, if we wish.