Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 71 ยท Components and Interaction

View Props and the Touch Responder System

Learn the useful props of React Native's View: pointerEvents, onLayout, collapsable, accessibility and focus props, and how the touch responder system picks a winner.

Core Components introduced View as the box you build layouts from. It has a surprising number of props beyond style, and one family of them, the responder props, is the machinery under every button, scroll view and gesture in React Native. You will not use most of these props every day, but when a tap goes to the wrong element, a screen reader skips your content or a shadow looks wrong, the answer is usually here.

What runs on this site. Touch handling is native, so the real React Native code is read-only. The runnable examples are stand-ins: they implement the same rules (who wins a touch, which elements can be tapped) as plain JavaScript and show the result as HTML.

pointerEvents: who may receive touches

pointerEvents decides whether a view and its children can be the target of a touch. It is available as a prop and, in current versions, as a style value too.

Value The view itself Its children
"auto" (default) can be the target can be the target
"none" cannot cannot
"box-none" cannot can
"box-only" can cannot

"none" is the right tool for decoration: a gradient overlay on top of a button must not swallow the tap. "box-none" is for a full-screen overlay container that holds a few tappable controls: taps on the empty parts pass through to what is underneath, while the buttons still work. "box-only" is the rare opposite: a card that should handle the touch itself, ignoring touches on its labels and icons.

JSX
// A full-screen layer with one floating button
<View style={StyleSheet.absoluteFill} pointerEvents="box-none">
  <Pressable style={styles.fab} onPress={add}>
    <Text>+</Text>
  </Pressable>
</View>
Which element receives the tap? (pointerEvents stand-in)

Try Overlay: none. Everything below it is blocked, so the tap falls through to whatever lies beneath the overlay. With box-none the overlay lets the card and button work while its own empty area stays transparent to touches.

onLayout: measuring after layout

onLayout runs after a view has been laid out and gives you its position and size:

JSX
<View
  onLayout={(event) => {
    const { x, y, width, height } = event.nativeEvent.layout;
    setBoxWidth(width);
  }}
/>

It fires on mount and again whenever the size or position changes (rotation, new content, keyboard). Two rules matter. The x and y are relative to the parent, not the screen. And do not set state inside onLayout blindly: setting a value that changes the layout again can loop. Compare with the old value first. For the window size use useWindowDimensions instead (see Dimensions and Responsive Layout); use onLayout when you need the size of one element, for example to size a chart to its container.

Props that tune rendering

  • collapsable (Android): React Native removes views that only exist for layout, to keep the native tree small. A view that you must measure or reference natively needs collapsable={false}, otherwise there is no native view to find. collapsableChildren controls the same for the children.
  • removeClippedSubviews: detaches off-screen children from the native tree; used internally by lists.
  • needsOffscreenAlphaCompositing (Android): fixes the case where a view with opacity less than 1 and overlapping children shows seams. It costs memory, so use it only for the bug.
  • shouldRasterizeIOS: renders the view into a bitmap and reuses it; good for complex static content that animates (scale or move), bad for content that changes.
  • renderToHardwareTextureAndroid: the Android counterpart, useful during an animation of a complex view.
  • hitSlop: expands the tap area beyond the box, for small icons (see Touchables and Pressable).
  • nativeID and testID: stable identifiers for native lookups and for tests.

Accessibility and focus props

A View is invisible to screen readers unless you say otherwise. The core props are accessible (group the children into one focusable item), accessibilityLabel, accessibilityHint, accessibilityRole, accessibilityState and accessibilityValue; they are explained in Accessibility in React Native. The less common ones:

Prop Platform Purpose
accessibilityActions and onAccessibilityAction both custom actions in the screen reader's actions menu (for example "delete" on a row)
onAccessibilityTap iOS called when the user activates the element with the screen reader
onMagicTap iOS the two-finger double tap, usually play or pause
onAccessibilityEscape iOS the two-finger scrub gesture, which should dismiss or go back
accessibilityIgnoresInvertColors iOS keep images and videos from being colour-inverted by Smart Invert
importantForAccessibility Android "yes", "no", "no-hide-descendants"
nextFocusUp, Down, Left, Right, Forward Android the next element when navigating with a keyboard, a remote or a TV

The responder system

Under every Pressable and gesture is the responder system. A touch can land on a screen with many nested views, but only one view may "own" it. Views compete by answering two kinds of questions with true or false:

Prop The question
onStartShouldSetResponder "A finger just touched. Do you want to be the responder?"
onMoveShouldSetResponder "The finger moved. Do you want to take over?"
onStartShouldSetResponderCapture the same as the first, but asked before the children
onMoveShouldSetResponderCapture the same as the second, asked before the children

The negotiation goes in two passes. The capture pass asks from the outermost view inward, so a parent can steal a touch before its children see it. The bubble pass then asks from the innermost view outward, so the deepest view gets the first chance, and the first true wins.

The winner gets a series of calls: onResponderGrant (you own the touch), onResponderMove (the finger moved), and finally onResponderRelease (the finger lifted). A competitor can interrupt: onResponderTerminationRequest asks the current owner if it may give up (return false to refuse), and onResponderTerminate tells it that it lost the touch. onResponderReject tells a view that asked and was refused.

JSX
<View
  onStartShouldSetResponder={() => true}
  onResponderGrant={() => setActive(true)}
  onResponderMove={(e) => setX(e.nativeEvent.locationX)}
  onResponderRelease={() => setActive(false)}
  onResponderTerminationRequest={() => false}   // do not let others steal it
/>

Each handler receives an event with nativeEvent.locationX, locationY (relative to the view), pageX, pageY (relative to the screen) and timestamp.

This low-level API is why a ScrollView and a Pressable can live together: the press yields to the scroll when the finger moves far enough (onResponderTerminationRequest returns true for a button). In practice you use PanResponder (a helper built on these props) or, better, the gesture libraries in Reanimated and Gestures. You read the raw props when debugging "my button stops working inside a swipeable row".

Capture and bubble: who wins the touch? (stand-in)

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. You put a transparent full-screen View over your app to host one floating button. Which pointerEvents value on the container keeps the rest of the app tappable?

    box-none makes the container itself ignore touches while its children (the button) still receive them. none would disable the button too.
  2. 2. What are the x and y values in onLayout relative to?

    The layout rectangle is measured relative to the view's parent. Use measureInWindow when you need screen coordinates.
  3. 3. In the responder system, which pass asks the outermost view first?

    Capture handlers run from the outside in, so a parent can take a touch before its children; the bubble pass then runs from the inside out.
  4. 4. Why would you set collapsable={false} on a View?

    React Native may remove views that only exist to lay out children. collapsable={false} keeps the native view so native measuring and references work.

Summary

  • pointerEvents has four values: auto, none, box-none (children only) and box-only (the box only). Use none for decorative overlays and box-none for overlay containers.
  • onLayout reports position and size after layout, relative to the parent; guard against loops.
  • collapsable, shouldRasterizeIOS, needsOffscreenAlphaCompositing and the hardware-texture prop are rendering fixes, used for a specific problem.
  • Accessibility props such as accessibilityActions, onMagicTap and nextFocus* serve screen readers and keyboard or TV focus.
  • Only one view owns a touch. Capture runs outside in, bubble runs inside out, and the first true wins; the owner then receives grant, move and release.

Next: Touchables and Pressable.