Button, which you met in the first lesson, draws a fixed platform look: it takes no style prop and no children. When a design needs an icon or a different look while pressed, you build the button yourself with Pressable, a wrapper that reports each stage of a touch and leaves the drawing to you.

Key terms

  • Pressable: a core component wrapper that detects the stages of a press on any children it wraps.
  • onPressIn and onPressOut: the callbacks Pressable calls when a press is activated and when it is deactivated.
  • onLongPress: the callback Pressable calls while the finger is still down, once a press lasts longer than delayLongPress (500 ms by default).
  • HitRect: the area around the element where a touch can register and a press can start, set with hitSlop.
  • PressRect: the area a press that has already started can move within and stay active, set with pressRetentionOffset.

Where Button stops

Button covers a plain text action. It renders as a blue label on iOS and as a blue rounded rectangle with light text on Android. Its color prop sets the text color on iOS and the background color on Android, and apart from disabled, that is all the control you get over its look. Button always renders its title inside its own View and Text, so there is nowhere to put an icon.

Pressable accepts View props, style included, on top of its own press props, and it renders whatever children you give it. When Button doesn't look right for your app, build your own button with it.

Pressable is also preferred over the Touchable components: TouchableOpacity, TouchableHighlight, TouchableWithoutFeedback and TouchableNativeFeedback. The reference page of each one points you to Pressable for handling touch input.

Which callback fires when

The callbacks run in this order:

  1. onPressIn runs as soon as a touch is engaged.
  2. When the finger lifts, onPressOut and onPress run.
  3. If the finger stays down longer than 500 ms without moving more than 10 points from where the press started, onLongPress runs while it is still down, and onPressOut still runs when the finger lifts. Moving farther cancels the long press. delayLongPress changes the 500 ms.

When onLongPress is set, a press that reaches the long-press delay skips onPress on release. Without onLongPress, nothing is skipped, and a long hold still ends in onPressOut and then onPress.

On release, the order depends on how long the press lasted. Pressable keeps every press active for at least 130 ms, and a press that lasts that long ends with onPressOut and then onPress. When the finger lifts sooner, onPress runs right away while onPressOut waits on a timer for the rest of the 130 ms, so a very quick tap logs onPress before onPressOut. Don't write an onPress handler that relies on onPressOut having run first.

If the finger slides out of the PressRect before lifting, the press deactivates and onPressOut runs, and lifting the finger out there calls no onPress. A disabled Pressable runs none of these callbacks.

Reading pressed in style and children

Pressable tracks a pressed state only when style or children is a function. It turns true when onPressIn fires and goes back to false when onPressOut fires, so the pressed look lasts at least 130 ms even for a quick tap.

Both functions receive an object holding pressed, so destructure it as ({pressed}) => .... Written as (pressed) => (pressed ? a : b), the callback gets the whole object, which is always truthy, so it always picks the pressed branch.

Growing the touch area with hitSlop

A number applies the same distance to all four sides, so hitSlop={10} lets a press start up to 10 beyond each edge of the element. pressRetentionOffset sets the PressRect and defaults to 20 on the top, left and right and 30 at the bottom.

The PressRect extends the HitRect, so a larger hitSlop also lets the finger slide farther before the press deactivates. pressRetentionOffset never changes where a press can start, because Pressable only uses it while the finger moves. When sibling views overlap, the one with the higher z-index gets the touch.

A parent left at the default overflow: 'visible' still lets a press start in the part of a child's hitSlop that reaches past the parent's edges. Set overflow: 'hidden' on the parent to keep the touch area inside it.

How SaveButton uses both functions

SaveButton, the code example below the lesson, reads pressed twice. Its style function switches the background from '#2563eb' to '#1e40af', and its children function turns the label '#bfdbfe' while the finger is down. hitSlop={8} lets a press start up to 8 beyond each edge of the button. On Android you can also pass android_ripple to turn on and configure the ripple effect.

Code Examples

tsx
import {Pressable, StyleSheet, Text} from 'react-native';

type SaveButtonProps = {
  onSave: () => void;
};

export function SaveButton({onSave}: SaveButtonProps) {
  return (
    <Pressable
      onPress={onSave}
      hitSlop={8}
      style={({pressed}) => [
        styles.button,
        {backgroundColor: pressed ? '#1e40af' : '#2563eb'},
      ]}>
      {({pressed}) => (
        <Text style={[styles.label, {color: pressed ? '#bfdbfe' : '#ffffff'}]}>Save</Text>
      )}
    </Pressable>
  );
}

const styles = StyleSheet.create({
  button: {paddingVertical: 12, paddingHorizontal: 20, borderRadius: 8},
  label: {fontSize: 16, fontWeight: '600'},
});
✓ Completed