React Native Pressable Component
Last Updated :
10 May, 2025
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.
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:
Similar Reads
React Native View Component
In this article, We are going to see how to create a view component in react-native. For this, we are going to use React Native View component. Itâs basically a small container that supports layout with flexbox, style, some touch handling, and accessibility controls. View maps directly to the native
6 min read
React Native Smart Components
In this article, we are going to learn about Smart components in React Native. The smart component is one of the categories of React Components so before diving into the smart components detail. A Component is one of the core building blocks of React and React-Native. The component is a block of cod
3 min read
React Native Modal Component
The following approach covers how to create Modal in react-native. For this, we are going to use the Modal component. It is a basic way to present content above an enclosing view. Syntax: <Modal animationType="" transparent={} visible={} onRequestClose={}>Props in Modal: animationType: This pr
3 min read
React Native Touchables Component
In this article, We are going to see how to create a Touchables in react-native. For this, we are going to use Touchable components. It is used to make any component touchable. Syntax: <TouchableHighlight onPress={}> // Inside Components </TouchableHighlight>Components in Touchables: Tou
2 min read
React Native Text Input Component
In this article, We are going to see how to create a TextInput in react-native. For this, we are going to use the TextInput component. It is a basic component that is used to collect data from users. Let's watch a demo video of what we are going to develop. Demo Video To create a TextInput in react
11 min read
Dumb Components in React Native
In this article, we are going to learn about Dumb components in React Native. The dumb component is one of the categories of React Components, so before diving into the dumb component details. Let's know a little bit about components. A Component is one of the core building blocks of React. The comp
5 min read
How to Implement Radio Button In React Native ?
In this article, we will learn to implement a Radio Button in React Native. A radio button signifies a graphical user interface element enabling individuals to make an exclusive seÂlection among multiple alternativeÂs. React Native is a popular platform for creating native mobile apps for iOS and A
11 min read
How to Test React Components using Jest ?
React is a frontend open-source JavaScript library, used to build interactive User Interfaces. React is focused on Single Page applications and is more popularly known as a SPA. Testing is an important aspect of development. It examines whether the application is behaving as intended, and protects
6 min read
How to create a Surface in react-native ?
React native is a framework developed by Facebook for creating native-style apps for iOS & Android under one common language, JavaScript. Initially, Facebook only developed React Native to support iOS. However, with its recent support of the Android operating system, the library can now render m
3 min read
How to create Chip in react-native using react-native-paper ?
React native is a framework developed by Facebook for creating native-style apps for iOS & Android under one common language, JavaScript. Initially, Facebook only developed React Native to support iOS. However, with its recent support of the Android operating system, the library can now render m
2 min read