Lesson 17 of 60 – Pressable Component in React Native
28%

Pressable Component in React Native

The Pressable component is used to create interactive elements that respond when a user touches or presses them. It is commonly used for buttons, menu items, cards, links and other interactive parts of a mobile application.

Note: Pressable is a flexible React Native component for handling press interactions. You can use JavaScript event handlers such as onPress, onLongPress and onPressIn to respond to different user interactions.

1. What is Pressable?

Pressable is a React Native component that detects press interactions from the user.

import { Pressable } from "react-native";

It can be used to create custom buttons and other interactive components.

2. Importing Pressable

Import Pressable from the react-native package.

import { Pressable } from "react-native";

It can also be imported together with View and Text.

import {
    View,
    Text,
    Pressable
} from "react-native";

3. Basic Pressable Syntax

<Pressable
    onPress={() => alert("Hello")}
>

    <Text>
        Press Me
    </Text>

</Pressable>

The onPress event runs when the user presses the component.

4. onPress Event

The onPress prop is the most commonly used Pressable event.

function handlePress() {

    alert("Button Pressed!");

}

return (
    <Pressable onPress={handlePress}>

        <Text>
            Click Me
        </Text>

    </Pressable>
);

5. Pressable with Text

Text is commonly placed inside Pressable to create a button-like interface.

<Pressable
    onPress={() => alert("Welcome")}
>

    <Text>
        Welcome
    </Text>

</Pressable>

6. Styling Pressable

A Pressable can be styled using the style prop.

const styles = StyleSheet.create({

    button: {
        backgroundColor: "black",
        padding: 15,
        borderRadius: 8
    }

});
<Pressable
    style={styles.button}
    onPress={() => alert("Pressed")}
>

    <Text>
        Submit
    </Text>

</Pressable>

7. Styling the Button Text

The Text inside Pressable can have its own style.

const styles = StyleSheet.create({

    buttonText: {
        color: "white",
        textAlign: "center",
        fontWeight: "bold"
    }

});
<Pressable style={styles.button}>

    <Text style={styles.buttonText}>
        Submit
    </Text>

</Pressable>

8. Pressable with View

Pressable can contain a View and multiple child components.

<Pressable
    onPress={() => alert("Card Pressed")}
>

    <View style={styles.card}>

        <Text>
            Student Profile
        </Text>

        <Text>
            Open Profile
        </Text>

    </View>

</Pressable>

This approach is useful for creating clickable cards.

9. Press Feedback

A mobile application should provide visual feedback when the user presses an interactive element.

Pressable supports a function-based style prop that receives information about the current press state.

<Pressable
    style={({ pressed }) => ({
        opacity: pressed ? 0.5 : 1
    })}
>

    <Text>
        Press Me
    </Text>

</Pressable>

10. The pressed Value

The pressed value tells you whether the Pressable is currently being pressed.

style={({ pressed }) => ({

    backgroundColor:
        pressed ? "gray" : "black"

})}

This can be used to change the appearance while the user is pressing the element.

11. onPressIn

The onPressIn event is called when the press interaction starts.

<Pressable
    onPressIn={() => {
        console.log("Press started");
    }}
>

    <Text>
        Press Me
    </Text>

</Pressable>

12. onPressOut

The onPressOut event is called when the press interaction ends.

<Pressable
    onPressOut={() => {
        console.log("Press ended");
    }}
>

    <Text>
        Press Me
    </Text>

</Pressable>

13. onLongPress

The onLongPress event runs when the user holds the Pressable for a longer period.

<Pressable
    onLongPress={() => {
        alert("Long Press");
    }}
>

    <Text>
        Hold Me
    </Text>

</Pressable>

14. delayLongPress

The delayLongPress prop can be used to specify how long the user needs to press before the long-press event is triggered.

<Pressable
    onLongPress={() => {
        alert("Long Press");
    }}
    delayLongPress={1000}
>

    <Text>
        Hold for a Moment
    </Text>

</Pressable>

The value is specified in milliseconds.

15. Pressable with Alert

Pressable can be used with an alert for simple demonstrations.

import {
    Alert,
    Pressable,
    Text
} from "react-native";

<Pressable
    onPress={() => {
        Alert.alert(
            "Message",
            "Button pressed!"
        );
    }}
>

    <Text>
        Show Message
    </Text>

</Pressable>

16. Pressable with a Function

Instead of writing the complete function inside onPress, you can define a separate function.

function handleSubmit() {

    console.log("Form submitted");

}

return (
    <Pressable
        onPress={handleSubmit}
    >

        <Text>
            Submit
        </Text>

    </Pressable>
);

17. Passing Data to a Function

You can pass values to a function using an arrow function.

function showStudent(name) {

    console.log(name);

}

return (
    <Pressable
        onPress={() =>
            showStudent("Rahul")
        }
    >

        <Text>
            Rahul
        </Text>

    </Pressable>
);

18. Pressable with State

Pressable can update React state when the user interacts with it.

import { useState } from "react";

const [count, setCount] =
    useState(0);

return (
    <View>

        <Text>
            Count: {count}
        </Text>

        <Pressable
            onPress={() =>
                setCount(count + 1)
            }
        >

            <Text>
                Increase
            </Text>

        </Pressable>

    </View>
);

19. Creating a Counter Button

A simple counter demonstrates how Pressable and state work together.

const [count, setCount] =
    useState(0);

<Text>
    {count}
</Text>

<Pressable
    style={styles.button}
    onPress={() =>
        setCount(count + 1)
    }
>

    <Text style={styles.buttonText}>
        + Add
    </Text>

</Pressable>

20. Disabled Pressable

A Pressable can be disabled when the user should not be able to interact with it.

<Pressable
    disabled={true}
    onPress={() => {
        alert("Pressed");
    }}
>

    <Text>
        Disabled
    </Text>

</Pressable>

When disabled, the press interaction is not triggered.

21. Dynamic Disabled State

The disabled prop can be controlled using state or other application conditions.

const [disabled, setDisabled] =
    useState(false);

<Pressable
    disabled={disabled}
    onPress={() => {
        console.log("Pressed");
    }}
>

    <Text>
        Submit
    </Text>

</Pressable>

22. Pressable as a Custom Button

Pressable is commonly used to build custom buttons instead of depending only on a standard button appearance.

const styles = StyleSheet.create({

    button: {
        backgroundColor: "black",
        padding: 15,
        borderRadius: 10,
        marginTop: 20
    },

    buttonText: {
        color: "white",
        textAlign: "center",
        fontSize: 18,
        fontWeight: "bold"
    }

});
<Pressable
    style={styles.button}
    onPress={() =>
        alert("Submitted")
    }
>

    <Text style={styles.buttonText}>
        Submit
    </Text>

</Pressable>

23. Pressable as a Clickable Card

A complete card can be made interactive by wrapping it with Pressable.

<Pressable
    onPress={() =>
        alert("Course selected")
    }
>

    <View style={styles.card}>

        <Text style={styles.title}>
            React Native
        </Text>

        <Text>
            Learn Mobile App Development
        </Text>

    </View>

</Pressable>

24. Pressable with Icon

Pressable can contain an icon and text together.

<Pressable
    style={styles.button}
    onPress={() =>
        console.log("Profile")
    }
>

    <Text style={styles.buttonText}>
        👤 Profile
    </Text>

</Pressable>

You can also use an icon library when your project includes one.

25. Pressable for Navigation

Pressable can trigger navigation when a navigation library is being used in the application.

<Pressable
    onPress={() =>
        navigation.navigate("Profile")
    }
>

    <Text>
        Open Profile
    </Text>

</Pressable>

The exact navigation code depends on the navigation setup used by the application.

26. Pressable for Forms

Pressable can be used for actions such as submitting forms.

<Pressable
    style={styles.button}
    onPress={() => {

        console.log(
            "Form submitted"
        );

    }}
>

    <Text style={styles.buttonText}>
        Submit Form
    </Text>

</Pressable>

27. Complete Pressable Example

import {
    View,
    Text,
    Pressable,
    StyleSheet
} from "react-native";

export default function App() {

    function handlePress() {

        alert("Button Pressed!");

    }

    return (
        <View style={styles.container}>

            <Text style={styles.title}>
                Student Dashboard
            </Text>

            <Pressable
                style={({ pressed }) => [

                    styles.button,

                    {
                        opacity:
                            pressed ? 0.6 : 1
                    }

                ]}
                onPress={handlePress}
                onLongPress={() =>
                    alert("Long Press")
                }
            >

                <Text style={styles.buttonText}>
                    Open Dashboard
                </Text>

            </Pressable>

        </View>
    );
}

const styles = StyleSheet.create({

    container: {
        flex: 1,
        justifyContent: "center",
        padding: 20
    },

    title: {
        fontSize: 26,
        fontWeight: "bold",
        textAlign: "center",
        marginBottom: 20
    },

    button: {
        backgroundColor: "black",
        padding: 16,
        borderRadius: 10
    },

    buttonText: {
        color: "white",
        fontSize: 17,
        fontWeight: "bold",
        textAlign: "center"
    }

});

28. Pressable vs Button

Pressable Button
Highly customizable Simple built-in button
Can customize pressed state Limited visual customization
Supports multiple press events Primarily uses onPress
Can contain custom UI Simple button interface
Useful for custom interactions Useful for simple buttons

Pressable is especially useful when you need more control over the appearance and behavior of an interactive element.

29. Best Practices for Pressable

  • Use meaningful press actions.
  • Provide clear visual feedback.
  • Use onPress for normal tap actions.
  • Use onLongPress only when long-press behavior is useful.
  • Disable the component when an action should not be available.
  • Keep button text clear and concise.
  • Make interactive areas easy to touch.
  • Consider accessibility for interactive elements.
  • Use reusable custom button components when the same design appears repeatedly.

30. Complete Pressable Workflow

Pressable
 ↓
Create Interactive Element
 ↓
Add onPress
 ↓
Add Styling
 ↓
Add Press Feedback
 ↓
Handle onPressIn / onPressOut
 ↓
Optional onLongPress
 ↓
Use State if Required
 ↓
Disable When Necessary
 ↓
Create Interactive UI

Pressable is one of the most useful components for creating custom interactive interfaces. You will use it for buttons, cards, menus, actions and many other interactive elements throughout a React Native application.

📌 Key Points

  • Pressable is used to create interactive elements.
  • onPress handles normal press actions.
  • onPressIn runs when the press begins.
  • onPressOut runs when the press ends.
  • onLongPress handles long-press interactions.
  • The pressed value can be used for visual feedback.
  • The disabled prop can disable interaction.
  • Pressable can contain Text, View, Image and other components.
  • It is useful for custom buttons and clickable cards.
  • Pressable provides more interaction and styling control than a simple button.

🧠 Quick Quiz

Question: Which Pressable event is normally used for a standard tap or press action?