Open In App

React Native Pressable Component

Last Updated : 10 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will see how to use the Pressable component of React Native. Pressable acts as a wrapper for the component inside it and uses React Native's Pressability API. 

In our project, we will wrap text and image separately with the Pressable component to demonstrate its functionality. Let’s watch a short video to see what we are going to create.

Demo Video:


Step-by-Step Implementation

Step 1: Create a React Native Project

Now, create a project with the following command.

npx create-expo-app app-name --template

Note: Replace the app-name with your app name for example : react-native-demo-app

Next, you might be asked to choose a template. Select one based on your preference as shown in the image below. I am selecting the blank template because it will generate a minimal app, as clean as an empty canvas in JavaScript.


It completes the project creation and displays a message: "Your Project is ready!" as shown in the image below.


Now go into your project folder, i.e., react-native-demo

cd app-name

Project Structure:


Step 2: Run  Application

Start the server by using the following command.

npx expo start

Then, the application will display a QR code.

  • For the Android users,
    • For the Android Emulator, press "a" as mentioned in the image below.
    • For Physical Device, Download the "Expo Go" app from the Play Store. Open the app, and you will see a button labeled "Scan QR Code." Click that button and scan the QR code; it will automatically build the Android app on your device.
  • For iOS users, simply scan the QR code using the Camera app.
  • If you're using a web browser, it will provide a local host link that you can use as mentioned in the image below.


Step 3: Create a new component folder (optional)

You can create a new folder called "components" to organize all component files better, as mentioned in the image below. Alternatively, you can write the component code directly in App.js.

file_structure_of_components


Step 4: Working with PressableComponent.js

- Import libraries: Import required libraries at the top of the file.

JavaScript
// Import necessary libraries and components
import React from 'react';
// Import Text, View, StyleSheet, Alert, Pressable, and Image from react-native
import { Text, View, StyleSheet, Alert, Pressable, Image } from 'react-native';


- StyleSheet: Create a StyleSheet to style components like text, container, and image.

JavaScript
// Define styles for the component
const styles = StyleSheet.create({
  text: {
    fontSize: 30, // Font size for the text
    fontWeight: 'bold', // Bold font weight
  },
  container: {
    padding: 100, // Padding around the container
    justifyContent: 'center', // Center content vertically
    alignItems: 'center', // Center content horizontally
  },
  image: {
    marginTop: 40, // Margin above the image
    height: 200, // Height of the image
    width: 200, // Width of the image
  },
});


- Pressable: This is a React Native component that makes its child components interactive for the user. For example, we can wrap a Text and an Image component with a Pressable component. When the user taps on either the Text or the Image, it will respond with an Alert message as specified in the code below.

JavaScript
{/* Pressable component with a Text element */}
<Pressable
    onPress={() => {
        
        Alert.alert('Text Pressable Example'); // Show an alert when pressed
        
        }}>
        
    <Text style={styles.text}>Press Me</Text>
    
</Pressable>


{/* Pressable component with an Image element */}
<Pressable
onPress={() => {
    
    Alert.alert('Image Pressable Example'); // Show an alert when pressed
    
    }}>
    
    <Image
    source={{
    uri: 'https://media.geeksforgeeks.org/wp-content/uploads/20220217151648/download3.png', // Image source URL
    }}
    style={styles.image} // Apply styles to the image
    />
    
</Pressable>


Now, wrap the two Pressable components with a View component and return from the PressableExample component. Also, ensure to export the PressableExample.

PressableComponent.js:

PressableComponent.js
import React from 'react';
import { Text, View, StyleSheet, Alert, Pressable, Image } from 'react-native';

// Define the PressableExample component
const PressableExample = () => {
  return (
    <View style={styles.container}>
      {/* Pressable component with a Text element */}
      <Pressable
        onPress={() => {
          console.log('Pressable Example'); // Log message to console
          Alert.alert('Text Pressable Example'); // Show an alert when pressed
        }}>
        <Text style={styles.text}>Press Me</Text>
      </Pressable>

      {/* Pressable component with an Image element */}
      <Pressable
        onPress={() => {
          Alert.alert('Image Pressable Example'); // Show an alert when pressed
        }}>
        <Image
          source={{
            uri: 'https://media.geeksforgeeks.org/wp-content/uploads/20220217151648/download3.png', // Image source URL
          }}
          style={styles.image} // Apply styles to the image
        />
      </Pressable>
    </View>
  );
};

export default PressableExample;

// Define styles for the component
const styles = StyleSheet.create({
  text: {
    fontSize: 30, // Font size for the text
    fontWeight: 'bold', // Bold font weight
  },
  container: {
    padding: 100, // Padding around the container
    justifyContent: 'center', // Center content vertically
    alignItems: 'center', // Center content horizontally
  },
  image: {
    marginTop: 40, // Margin above the image
    height: 200, // Height of the image
    width: 200, // Width of the image
  },
});


Step 5: Working with App.js

Now call this PressableExample Component in the main "App" Component in App.js.

App.js:

JavaScript
// Import React library to use React components
import React from 'react';
// Import View component from react-native for layout purposes
import { View } from 'react-native';
// Import the custom PressableExample component from the components folder
import PressableExample from './components/PressableComponent';

// Define the main App component
const App = () => {
  return (
    // Render a View container
    <View>
      {/* Render the PressableExample component inside the View */}
      <PressableExample />
    </View>
  );
};

// Export the App component as the default export of this file
export default App;


Or

You can write the whole code in one file, i.e, App.js.

Complete Source Code:

App.js:

JavaScript
// Import necessary libraries and components
import React from 'react';
// Import Text, View, StyleSheet, Alert, Pressable, and Image from react-native
import { Text, View, StyleSheet, Alert, Pressable, Image } from 'react-native';

// Define the main App component
const App = () => {
  return (
    // Render a View container
    <View>
      {/* Render the PressableExample component inside the View */}
      <PressableExample />
    </View>
  );
};

// Define the PressableExample component
const PressableExample = () => {
  return (
    <View style={styles.container}>
      {/* Pressable component with a Text element */}
      <Pressable
        onPress={() => {
          console.log('Pressable Example'); // Log message to console
          Alert.alert('Text Pressable Example'); // Show an alert when pressed
        }}>
        <Text style={styles.text}>Press Me</Text>
      </Pressable>

      {/* Pressable component with an Image element */}
      <Pressable
        onPress={() => {
          Alert.alert('Image Pressable Example'); // Show an alert when pressed
        }}>
        <Image
          source={{
            uri: 'https://media.geeksforgeeks.org/wp-content/uploads/20220217151648/download3.png', // Image source URL
          }}
          style={styles.image} // Apply styles to the image
        />
      </Pressable>
    </View>
  );
};
// Export the App component as the default export of this file
export default App;

// Define styles for the component
const styles = StyleSheet.create({
  text: {
    fontSize: 30, // Font size for the text
    fontWeight: 'bold', // Bold font weight
  },
  container: {
    padding: 100, // Padding around the container
    justifyContent: 'center', // Center content vertically
    alignItems: 'center', // Center content horizontally
  },
  image: {
    marginTop: 40, // Margin above the image
    height: 200, // Height of the image
    width: 200, // Width of the image
  },
});


Output:



Next Article

Similar Reads