React Native

React Native | Pressable

Hello React Native Friends,

In today’s blog, I will explain about Pressable component in React Native.

React Native provides several components to handle touch interactions, such as TouchableOpacity, TouchableHighlight, and TouchableWithoutFeedback. However, with the introduction of Pressable, developers now have a more versatile and performant way to handle press events.

What is Pressable?

Pressable is a core component in React Native that allows you to detect various touch interactions, such as press, long press, and press-in/out events. It offers more control and flexibility compared to its predecessors, making it a preferred choice for modern React Native applications.

<Pressable onPress={onPressFunction}>
  <Text>I'm pressable!</Text>
</Pressable>

Props of Pressable

Pressable comes with a variety of props that allow you to customize its behavior:

  • onPress: Function to handle the press event.
  • onLongPress: Function to handle the long press event.
  • onPressIn: Function to handle the press-in event.
  • onPressOut: Function to handle the press-out event.
  • disabled: Boolean to disable the Pressable.
  • hitSlop: Object or number to increase the pressable area.
  • style: Style object or function to apply styles based on the press state.
  • android_ripple: Object to configure ripple effect on Android.

Example:

import React, {useState} from 'react';
import {Pressable, StyleSheet, Text, View} from 'react-native';

const App = () => {
  const [timesPressed, setTimesPressed] = useState(0);

  let textLog = '';
  if (timesPressed > 1) {
    textLog = timesPressed + 'x onPress';
  } else if (timesPressed > 0) {
    textLog = 'onPress';
  }

  return (
    <View style={styles.container}>
     <Pressable
        onPress={() => Alert.alert('Button Pressed')}
        onLongPress={() => Alert.alert('Button Long Pressed')}
        onPressIn={() => console.log('Press In')}
        onPressOut={() => console.log('Press Out')}
        style={({ pressed }) => [
          {
            backgroundColor: pressed ? 'rgb(210, 230, 255)' : 'white'
          },
          styles.button
        ]}
        hitSlop={{ top: 10, bottom: 10, left: 10, right: 10 }}
        android_ripple={{ color: 'blue', borderless: false }}
        disabled={false}
      >
        <Text style={styles.text}>Press Me</Text>
      </Pressable>
   
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
  },
  text: {
    fontSize: 16,
  },
  wrapperCustom: {
    borderRadius: 8,
    padding: 6,
  },
  logBox: {
    padding: 20,
    margin: 10,
    borderWidth: StyleSheet.hairlineWidth,
    borderColor: '#f0f0f0',
    backgroundColor: '#f9f9f9',
  },
});

export default App;

Conclusion:

Pressable is a powerful and flexible component for handling touch interactions in React Native applications. It provides better control, performance, and customization options compared to its predecessors. By incorporating Pressable into your mobile app, you can enhance the user experience and ensure your app remains responsive and intuitive.

Whether you’re building a simple button or a complex gesture-based interface, Pressable is a valuable tool in your React Native toolkit. Hire a React Native Developer to give it a try in your next project and see the difference it makes!

Happy Coding!

Click to rate this post!
[Total: 0 Average: 0]
Bharat Desai

Bharat Desai is a Co-Founder at MageComp. He is an Adobe Magento Certified Frontend Developer ? with having 8+ Years of experience and has developed 150+ Magento 2 Products with MageComp. He has an unquenchable thirst to learn new things. On off days you can find him playing the game of Chess ♟️ or Cricket ?.

Share
Published by
Bharat Desai

Recent Posts

Organic Traffic Drop: Sudden Decline Causes and Solutions

A sudden drop in organic traffic can be alarming for any business. Organic traffic is…

10 hours ago

Difference Between: Magento 2 WhatsApp Order Notification vs. Magento 2 SMS Notification

In the fast-paced world of eCommerce, effective communication is key to maintaining customer satisfaction and…

10 hours ago

Understanding useSearchParams vs useParams Hooks in Remix

In the Remix framework, handling URL parameters is a common task when building dynamic web…

1 day ago

How to Implement Frontend Design Customization in Shopify Remix App?

In this blog post, we'll show you how to implement frontend design customization in the…

3 days ago

Your Ultimate Guide to Shopify Headless Commerce

Starting an eCommerce business with Shopify can be easy yet challenging in many different ways,…

5 days ago

Magento 2: How to Show Custom Notice Message Before Payment Step on Checkout

Hello Magento Friends, In Magento 2, you can customize the checkout process to enhance the…

5 days ago