When a finger lands on the screen, the app can't yet tell whether it starts a scroll, a slide on a control or a tap, and the answer can change while the finger is still down. React Native settles this with the gesture responder system: views negotiate which one handles the touch through props on View, without knowing anything about their parents or children. The Touchable components such as TouchableHighlight are built on it for plain taps, while a custom drag talks to these props directly.

Key terms

  • Responder: the view that currently handles a touch and receives its move and release events.
  • onStartShouldSetResponder: the View prop asked at the start of a touch whether this view wants to become the responder.
  • onResponderGrant: the callback a view receives when the responder system picks it for the touch.
  • Capture phase: the pass that asks parents, through the on*ShouldSetResponderCapture props, before the deepest view gets its say.
  • GestureResponderEvent: the type of the synthetic touch event every responder prop receives, with the touch data under nativeEvent.

Asking for the touch

A view takes part by answering one of two questions. onStartShouldSetResponder runs when a touch starts, and onMoveShouldSetResponder runs on every touch move while the view is not the responder. Returning true from either one asks for the touch.

When no other view holds the touch, the view gets onResponderGrant and becomes the responder, which is the moment to highlight it so the user sees the touch registered. When another view holds it, React Native still calls onResponderGrant on the asking view first, and only then asks the current responder through onResponderTerminationRequest. If the current responder refuses, the asking view then receives onResponderReject, so a grant is not final until no reject follows. Undo in onResponderReject whatever the view started in onResponderGrant.

When several views want the same touch, the questions bubble: the deepest view is asked first, and the first handler that returns true picks the responder. The handlers further up are not called at all. A button inside a card wins over the card, which keeps every nested control usable.

Holding the touch, and losing it

Once a view is the responder, four callbacks can follow:

  • onResponderMove: the finger is moving.
  • onResponderRelease: the touch ended (touchUp).
  • onResponderTerminationRequest: another view wants to become the responder. Return true to let it.
  • onResponderTerminate: the role was taken away.

Termination is not always negotiated. It can follow a termination request that your view accepted, but the OS can also take the responder without asking, for example when the user pulls down Control Center or Notification Center on iOS. Treat onResponderTerminate as a cancel and undo whatever onResponderGrant started, the same way you would on release.

Leaving onResponderTerminationRequest out has the same effect as returning true: the view gives up the touch whenever another view asks for it. To keep the touch, return false, and the view that asked receives onResponderReject. On Android, returning true from onResponderGrant stops other native components from becoming the responder until this one terminates.

Parents that claim the touch first

Bubbling favors the deepest view, which is the wrong default when a parent must own the gesture. Before bubbling starts, the responder system runs a capture phase with two props of its own: onStartShouldSetResponderCapture and onMoveShouldSetResponderCapture. Capture handlers are called from the root down to the touched view, then the bubbling handlers run from that view back up, so a parent that returns true from a capture handler takes the touch before its child is asked.

A horizontal carousel of tappable cards needs both phases. A tap should reach the card, so the carousel has no onStartShouldSetResponderCapture and the card becomes the responder when the touch starts. Once the finger moves, the carousel's onMoveShouldSetResponderCapture returns true. The card, as the current responder, receives onResponderTerminationRequest, and if it allows the release it gets onResponderTerminate while the carousel becomes the responder.

What a touch event carries

Every responder callback receives a synthetic touch event typed GestureResponderEvent. Its nativeEvent holds the touch data, typed NativeTouchEvent (the reference page calls it PressEvent):

  • locationX, locationY: the position of the touch relative to the element.
  • pageX, pageY: the position relative to the root view.
  • timestamp: when the event happened, in milliseconds, which is what velocity math needs.
  • identifier: the ID of the touch.
  • target: the node id of the element receiving the event.
  • touches and changedTouches: every touch currently on screen, and the touches that changed since the last event.

On iOS there is also force, a float from 0.0 to 1.0 on 3D Touch hardware.

The SwipeMeter component below shows how far a finger travels across a view, using only these props. It stores the starting position when it becomes the responder:

startX.current = event.nativeEvent.pageX;

Each onResponderMove then subtracts that start from the current pageX. Measure distance with pageX rather than locationX: locationX is relative to the element, so it shifts whenever the element itself moves under the finger. The meter refuses to hand the touch over with onResponderTerminationRequest={() => false}, and it still resets in onResponderTerminate, because the OS can take the touch without asking.

Code Examples

tsx
import {useRef, useState} from 'react';
import {StyleSheet, Text, View} from 'react-native';
import type {GestureResponderEvent} from 'react-native';

export function SwipeMeter() {
  const startX = useRef(0);
  const [distance, setDistance] = useState(0);
  const [active, setActive] = useState(false);

  const reset = () => {
    setActive(false);
    setDistance(0);
  };

  return (
    <View
      style={[styles.meter, active && styles.active]}
      onStartShouldSetResponder={() => true}
      onResponderGrant={(event: GestureResponderEvent) => {
        startX.current = event.nativeEvent.pageX;
        setActive(true);
      }}
      onResponderMove={(event: GestureResponderEvent) => {
        setDistance(event.nativeEvent.pageX - startX.current);
      }}
      onResponderTerminationRequest={() => false}
      onResponderRelease={reset}
      onResponderTerminate={reset}>
      <Text>{Math.round(distance)}</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  meter: {
    height: 120,
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: '#e5e7eb',
  },
  active: {
    backgroundColor: '#bfdbfe',
  },
});
✓ Completed