When we are making a good product on any digital platform at that time, we have to provide some different features and functionalities which can make our product more productive and look more efficient compared to other products. So providing swipe and gesture features in the app will make our app more user-friendly. So users can easily attract and perform an interaction with our app.
In this article, I will explain how we can create a swipeable component in react native.
There are many libraries and SDKs which provide swipe and gesture components and facilities.
➡️ We will use react-native-gesture-handler because it’s the most used and popular library. It Provides platform-specific touches and gestures for React native platform. You can install using the below command :
yarn add react-native-gesture-handler cd ios && pod install
➡️ We have to wrap our whole app with GestureHandlerRootView so we can use the library’s gesture and swipe actions and methods.
➡️ Now we will move to make a swipeable component using react-native-gesture-handler’s Swipeable component.
The Swipable component allows you to create horizontal actions through which you can swipe left and right.
So In this demo, we will make a list and in that, we can create left and right swipeable actions.
➡️ So first we need to import the Swipeable component from the react-native-gesture-handler library given below:
import Swipeable from 'react-native-gesture-handler/Swipeable';
The Swipeable component has many properties as given here. But here for making a demo, we will use some basic required properties only like –
When we want to provide swipeable actions on the right side of a component so the user can swipe right to left and execute an action, the renderRightActions method can be useful.
The renderLeftActions function can be used to accomplish the same type of action. The swipeable panel can be dragged beyond the width of the right action panel if the overshootRight prop is given a boolean value. We can do the same for overshootLeft.
So we can write code for swipeable like given below:
return ( <Swipeable overshootRight={false} overshootLeft={false} renderRightActions={(progress, dragX) => renderRightActions(progress, dragX, onPressDelete) } renderLeftActions={(progress, dragX) => renderLeftActions(progress, dragX, onMarkRead) } containerStyle={styles.swipableItemMainView}> <View style={styles.itemWrap}> <Text style={styles.title}>{notification?.title}</Text> <Text style={styles.createdAt}>{notification?.createdAt}</Text> </View> </Swipeable> );
While rendering left and right actions we have to use animated components so it looks more creative. You can write similar functions to render actions like those given below.
const renderRightActions = (progress, dragX, onPressDelete) => { const scale = dragX.interpolate({ inputRange: [-100, -50, 0], outputRange: [2, 1, 0], extrapolate: 'clamp', }); return ( <TouchableOpacity activeOpacity={0.8} onPress={() => onPressDelete(index, notification)} style={styles.rightAction}> <Animated.Image color={'white'} source={deleteIcon2} style={[{transform: [{scale}]}]} /> </TouchableOpacity> ); };
const renderLeftActions = (progress, dragX, onMarkRead) => { const trans = dragX.interpolate({ inputRange: [0, 50, 100, 101], outputRange: [-20, 0, 0, 1], }); return ( <TouchableOpacity activeOpacity={0.8} onPress={() => onMarkRead(index, notification)} style={styles.leftAction}> <Animated.Image color={'white'} source={markReadIcon} style={[{transform: [{translateX: trans}]}]} /> </TouchableOpacity> ); };
So now to make a proper demo we will use a sample list and use this Swipeable component. So our app App.js will look like the given below:
import {Alert, FlatList, StyleSheet, View} from 'react-native'; import React, {useState} from 'react'; import SwipableItem from './src/components/SwipableItem'; import {GestureHandlerRootView} from 'react-native-gesture-handler'; const App = () => { const [list, setList] = useState([ { title: 'This is the test notification', createdAt: '23 March 2023', }, { title: 'This is the test notification', createdAt: '22 March 2023', }, { title: 'This is the test notification', createdAt: '21 March 2023', }, { title: 'This is the test notification', createdAt: '20 March 2023', }, { title: 'This is the test notification', createdAt: '19 March 2023', }, ]); const renderListItem = ({item, index}) => { return ( <SwipableItem notification={item} index={index} onPressDelete={(index, notification) => { Alert.alert( 'Test App', 'Are you sure you want to delete this notification?', [ { text: 'No', onPress: () => null, }, { text: 'Yes', onPress: () => { let sampleList = list; sampleList = sampleList.filter((obj, i) => i != index); setList(sampleList); }, }, ], ); }} onMarkRead={(index, notification) => { console.log('onMarkRead index, notification', index, notification); }} /> ); }; return ( <GestureHandlerRootView style={styles.container}> <View style={styles.container}> <FlatList data={list} renderItem={renderListItem} contentContainerStyle={{flexGrow: 1, backgroundColor: 'white'}} /> </View> </GestureHandlerRootView> ); }; export default App; const styles = StyleSheet.create({ container: { flex: 1, paddingTop: 50, backgroundColor: 'white', }, });
So here we have wrapped our main app with GestureHandlerRootView to get all the library’s features with native touch and gestures. So we are considering a notification list and we want to delete a single notification using the swipe actions like the Gmail app.
Our final Swipeable component code will look like the given below:
import {Animated, StyleSheet, Text, TouchableOpacity, View} from 'react-native'; import React from 'react'; import Swipeable from 'react-native-gesture-handler/Swipeable'; const deleteIcon2 = require('../images/deleteIcon1.png'); const markReadIcon = require('../images/markReadIcon.png'); const SwipableItem = ({notification, onPressDelete, index, onMarkRead}) => { const renderRightActions = (progress, dragX, onPressDelete) => { const scale = dragX.interpolate({ inputRange: [-100, -50, 0], outputRange: [2, 1, 0], extrapolate: 'clamp', }); return ( <TouchableOpacity activeOpacity={0.8} onPress={() => onPressDelete(index, notification)} style={styles.rightAction}> <Animated.Image color={'white'} source={deleteIcon2} style={[{transform: [{scale}]}]} /> </TouchableOpacity> ); }; const renderLeftActions = (progress, dragX, onMarkRead) => { const trans = dragX.interpolate({ inputRange: [0, 50, 100, 101], outputRange: [-20, 0, 0, 1], }); return ( <TouchableOpacity activeOpacity={0.8} onPress={() => onMarkRead(index, notification)} style={styles.leftAction}> <Animated.Image color={'white'} source={markReadIcon} style={[{transform: [{translateX: trans}]}]} /> </TouchableOpacity> ); }; return ( <Swipeable overshootRight={false} overshootLeft={false} renderRightActions={(progress, dragX) => renderRightActions(progress, dragX, onPressDelete) } renderLeftActions={(progress, dragX) => renderLeftActions(progress, dragX, onMarkRead) } containerStyle={styles.swipableItemMainView}> <View style={styles.itemWrap}> <Text style={styles.title}>{notification?.title}</Text> <Text style={styles.createdAt}>{notification?.createdAt}</Text> </View> </Swipeable> ); }; export default SwipableItem; const styles = StyleSheet.create({ title: { fontSize: 14, color: 'black', fontWeight: '700', }, createdAt: { fontSize: 12, color: 'gray', fontWeight: '500', marginTop: 7, }, rightAction: { flex: 0.2, alignItems: 'center', justifyContent: 'center', backgroundColor: 'red', }, leftAction: { flex: 0.2, alignItems: 'center', justifyContent: 'center', backgroundColor: 'lightblue', }, itemWrap: { backgroundColor: 'pink', paddingVertical: 15, paddingHorizontal: 10, flex: 1, }, swipableItemMainView: { marginVertical: 10, marginHorizontal: 10, }, childrenContainerStyle: { borderRadius: 10, flexDirection: 'row', alignItems: 'center', backgroundColor: 'white', }, });
We can see in the given screenshot what we achieved using the Swipeable component.
Please click here to watch the demo video.
In this blog, I have covered the fundamental aspects of swipeable components, which can add a variety of functionalities to our app. These components come with numerous props and methods, enabling us to unlock a wide range of features.
By incorporating swipe and gesturing capabilities, we can attract diverse user groups and create a distinct experience with our product.
Let’s embrace the joy of coding and continue to create innovative solutions!😇
How To Effectively Hire And Manage A Remote Team Of Developers.
Download NowMaster Epic Integration with SMART on FHIR in Just 60 Minutes
Register HereMindbowser played a crucial role in helping us bring everything together into a unified, cohesive product. Their commitment to industry-standard coding practices made an enormous difference, allowing developers to seamlessly transition in and out of the project without any confusion....
CEO, MarketsAI
I'm thrilled to be partnering with Mindbowser on our journey with TravelRite. The collaboration has been exceptional, and I’m truly grateful for the dedication and expertise the team has brought to the development process. Their commitment to our mission is...
Founder & CEO, TravelRite
The Mindbowser team's professionalism consistently impressed me. Their commitment to quality shone through in every aspect of the project. They truly went the extra mile, ensuring they understood our needs perfectly and were always willing to invest the time to...
CTO, New Day Therapeutics
I collaborated with Mindbowser for several years on a complex SaaS platform project. They took over a partially completed project and successfully transformed it into a fully functional and robust platform. Throughout the entire process, the quality of their work...
President, E.B. Carlson
Mindbowser and team are professional, talented and very responsive. They got us through a challenging situation with our IOT product successfully. They will be our go to dev team going forward.
Founder, Cascada
Amazing team to work with. Very responsive and very skilled in both front and backend engineering. Looking forward to our next project together.
Co-Founder, Emerge
The team is great to work with. Very professional, on task, and efficient.
Founder, PeriopMD
I can not express enough how pleased we are with the whole team. From the first call and meeting, they took our vision and ran with it. Communication was easy and everyone was flexible to our schedule. I’m excited to...
Founder, Seeke
Mindbowser has truly been foundational in my journey from concept to design and onto that final launch phase.
CEO, KickSnap
We had very close go live timeline and Mindbowser team got us live a month before.
CEO, BuyNow WorldWide
If you want a team of great developers, I recommend them for the next project.
Founder, Teach Reach
Mindbowser built both iOS and Android apps for Mindworks, that have stood the test of time. 5 years later they still function quite beautifully. Their team always met their objectives and I'm very happy with the end result. Thank you!
Founder, Mindworks
Mindbowser has delivered a much better quality product than our previous tech vendors. Our product is stable and passed Well Architected Framework Review from AWS.
CEO, PurpleAnt
I am happy to share that we got USD 10k in cloud credits courtesy of our friends at Mindbowser. Thank you Pravin and Ayush, this means a lot to us.
CTO, Shortlist
Mindbowser is one of the reasons that our app is successful. These guys have been a great team.
Founder & CEO, MangoMirror
Kudos for all your hard work and diligence on the Telehealth platform project. You made it possible.
CEO, ThriveHealth
Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.
CEO, SMILINGMIND
They were a very responsive team! Extremely easy to communicate and work with!
Founder & CEO, TotTech
We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.
Co-Founder, TEAM8s
Mindbowser was very helpful with explaining the development process and started quickly on the project.
Executive Director of Product Development, Innovation Lab
The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.
Co-Founder, Vesica
Mindbowser is professional, efficient and thorough.
Consultant, XPRIZE
Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.
Founder, S.T.A.R.S of Wellness
Mindbowser was great; they listened to us a lot and helped us hone in on the actual idea of the app. They had put together fantastic wireframes for us.
Co-Founder, Flat Earth
Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.
Founder, Child Life On Call
The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!
CEO, SDOH2Health LLC
Mindbowser was easy to work with and hit the ground running, immediately feeling like part of our team.
CEO, Stealth Startup
Mindbowser was an excellent partner in developing my fitness app. They were patient, attentive, & understood my business needs. The end product exceeded my expectations. Thrilled to share it globally.
Owner, Phalanx
Mindbowser's expertise in tech, process & mobile development made them our choice for our app. The team was dedicated to the process & delivered high-quality features on time. They also gave valuable industry advice. Highly recommend them for app development...
Co-Founder, Fox&Fork