DEV Community

Brian Neville-O'Neill
Brian Neville-O'Neill

Posted on • Originally published at blog.logrocket.com on

How to make Tinder-like card animations with React Native

Written by Paweł Karniej✏️

Alt Text

Tinder has definitely changed the way people think about online dating thanks to its original swiping mechanism. Tinder was among the first “swiping apps” that heavily used a swiping motion for choosing the perfect match. Today we’ll build a similar solution in React Native.

Installation

The easiest way to replicate this swiping mechanism is to use react-native-deck-swiper. This is an awesome npm package opens up many possibilities. Let’s start by installing the necessary dependencies:

yarn add react-native-deck-swiper
yarn add react-native-view-overflow
yarn add react-native-vector-icons
Enter fullscreen mode Exit fullscreen mode

Although the newest React Native version (0.60.4, which we’re using in this tutorial) introduced autolinking, two of those three dependencies still have to be linked manually because, at the time of writing, their maintainers haven’t yet updated them to the newest version. So we have to link them the old-fashioned way:

react-native link react-native-view-overflow && react-native-link react-native-vector-icons
Enter fullscreen mode Exit fullscreen mode

Also, React Native version 0.60.0 and above uses CocoaPods by default for iOS, so one extra step is required to have everything installed correctly:

cd ios && pod install && cd ...
Enter fullscreen mode Exit fullscreen mode

After installation is complete, we can now run the app:

react-native run-ios
Enter fullscreen mode Exit fullscreen mode

If you’re having issues running app with the CLI, try opening XCode and build the app through it.

LogRocket Free Trial Banner

Building the Card.js component

After the installation is complete and we have the app running on a simulator, we can get to writing some code! We’ll start with a single Card component, which will display the photo and the name of person.

import React from 'react'
import { View, Text, Image, ImageSourcePropType } from 'react-native'
import { shape, string, number } from 'prop-types'
import styles from './Card.styles'
const Card = ({ card }) => (
  <View
    activeOpacity={1}
    style={styles.card}
  >
    <Image
      style={styles.image}
      source={card.photo}
      resizeMode="cover"
    />
    <View style={styles.photoDescriptionContainer}>
      <Text style={styles.text}>
        {`${card.name}, ${card.age}`}
      </Text>
    </View>
  </View>
)

Card.propTypes = { 
  card: shape({
    photo: ImageSourcePropType,
    name: string,
    age: number,
  }).isRequired,
}
export default Card
Enter fullscreen mode Exit fullscreen mode

I am using propTypes in this and in every project I work on in React Native. propTypes help a lot with the type safety of props passed to our component. Every wrong type of prop (e.g., string instead of number) will result in a console.warn warning inside our simulator.

When using isRequired for a specific propType, we’ll get an error inside a debugging console about missing props, which help us identify and fix errors quicker. I really recommend using propTypes from the prop-types library inside every component we write, using the isRequired option with every prop that’s necessary to render a component correctly, and creating a default prop inside defaultProps for every prop that doesn’t have to be required.

Styling our cards

Let’s keep going by styling the Card component. Here’s the code for our Card.styles.js file:

import { StyleSheet, Dimensions } from 'react-native'
import { colors } from '../../constants'
const { height } = Dimensions.get('window')
export default StyleSheet.create({
  card: {
    /* Setting the height according to the screen height, it also could be fixed value or based on percentage. In this example, this worked well on Android and iOS. */
    height: height - 300,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: colors.white,
    borderRadius: 5,
    shadowColor: colors.black,
    shadowOffset: {
      width: 0,
      height: 2,
    },
    shadowRadius: 6,
    shadowOpacity: 0.3,
    elevation: 2,
  },
  image: {
    borderRadius: 5,
    flex: 1,
    width: '100%',
  },
  photoDescriptionContainer: {
    justifyContent: 'flex-end',
    alignItems: 'flex-start',
    flexDirection: 'column',
    height: '100%',
    position: 'absolute',
    left: 10,
    bottom: 10,
  },
  text: {
    textAlign: 'center',
    fontSize: 20,
    color: colors.white,
    fontFamily: 'Avenir',
    textShadowColor: colors.black,
    textShadowRadius: 10,
  },
})
Enter fullscreen mode Exit fullscreen mode

Here’s how our card looks now:

Styled Card

IconButton.js component

The second component for our app renders the icon inside a colored, circular button, which is responsible for handling user interactions instead of swipe gestures ( Like , Star , and Nope ).

import React from 'react'
import { TouchableOpacity } from 'react-native'
import { func, string } from 'prop-types'
import Icon from 'react-native-vector-icons/AntDesign'
import styles from './IconButton.styles'
import { colors } from '../../constants'
const IconButton = ({ onPress, name, backgroundColor, color }) => (
  <TouchableOpacity
    style={[styles.singleButton, { backgroundColor }]}
    onPress={onPress}
    activeOpacity={0.85}
  >
    <Icon
      name={name}
      size={20}
      color={color}
    />
  </TouchableOpacity>
)
IconButton.defaultProps = {
  color: colors.white,
  backgroundColor: colors.heartColor,
}
IconButton.propTypes = {
  onPress: func.isRequired,
  name: string.isRequired,
  color: string,
  backgroundColor: string,
}
export default IconButton
Enter fullscreen mode Exit fullscreen mode

Styling our buttons

Now let’s get to styling:

import { StyleSheet } from 'react-native'

export default StyleSheet.create({
  singleButton: {
    backgroundColor: 'transparent',
    borderRadius: 50,
    alignItems: 'center',
    justifyContent: 'center',
    shadowColor: 'black',
    shadowOffset: {
      width: 0,
      height: 2,
    },
    shadowRadius: 6,
    shadowOpacity: 0.3,
    elevation: 2,
    padding: 15,
  },
})
Enter fullscreen mode Exit fullscreen mode

The three buttons will look like this:

Styled Buttons

OverlayLabel.js component

The OverlayLabel component is simple Text inside a View component with predefined styles.

import React from 'react'
import { View, Text } from 'react-native'
import { string } from 'prop-types'
import styles from './OverlayLabel.styles'

const OverlayLabel = ({ label, color }) => (
  <View style={[styles.overlayLabel, { borderColor: color }]}>
    <Text style={[styles.overlayLabelText, { color }]}>{label}</Text>
  </View>
)

OverlayLabel.propTypes = {
  label: string.isRequired,
  color: string.isRequired,
}

export default OverlayLabel
Enter fullscreen mode Exit fullscreen mode

Styling the OverlayLabel

And now the styling:

import { StyleSheet } from 'react-native'

export default StyleSheet.create({
  overlayLabel: {
    justifyContent: 'center',
    alignItems: 'center',
    padding: 10,
    borderWidth: 2,
    borderRadius: 10,
  },
  overlayLabelText: {
    fontSize: 25,
    fontFamily: 'Avenir',
    textAlign: 'center',
  },
})
Enter fullscreen mode Exit fullscreen mode

And here’s the result:

Styled Overlay Demo

Data

After creating those basic components, we have to create an array with objects to fill the Swiper component before we can build it. We’ll be using some free random photos found on Unsplash, which we’ll put inside the assets folder in the project folder root.

photoCards.js

const photoCards = [
  {
    name: 'Austin Wade',
    age: 22,
    photo: require('../assets/austin-wade-ex6qfO4TPMY-unsplash.jpg'),
    key: 'caseex6qfO4TPMYyhorner',
  },
  {
    name: 'Aleksander Borzenets',
    age: 28,
    photo: require('../assets/aleksander-borzenets-ozda-XbeP0k-unsplash.jpg'),
    key: 'ozda-XbeP0k',
  },
  {
    name: 'Don Delfin Espino',
    age: 29,
    photo: require('../assets/don-delfin-espino-nBywXevf_jE-unsplash.jpg'),
    key: 'nBywXevf_jE-',
  },
  {
    name: 'Eduardo Dutra',
    age: 30,
    photo: require('../assets/eduardo-dutra-ZHy0efLnzVc-unsplash.jpg'),
    key: 'ZHy0efLnzVc',
  },
  {
    name: 'Wesley Tingey',
    age: 21,
    photo: require('../assets/wesley-tingey-TvPCUHten1o-unsplash.jpg'),
    key: 'TvPCUHten1o',
  },
  {
    name: 'Gift Habeshaw',
    age: 26,
    photo: require('../assets/gift-habeshaw-dlbiYGwEe9U-unsplash.jpg'),
    key: 'dlbiYGwEe9U',
  },
  {
    name: 'Henri Pham',
    age: 30,
    photo: require('../assets/henri-pham-Ml4tr2WO7JE-unsplash.jpg'),
    key: 'Ml4tr2WO7JE',
  },
  {
    name: 'Nico Marks',
    age: 24,
    photo: require('../assets/nico-marks-mFcc5b_t74Q-unsplash.jpg'),
    key: 'mFcc5b_t74Q',
  },
  {
    name: 'Sirio',
    age: 28,
    photo: require('../assets/sirio-Ty4f_NOFO60-unsplash.jpg'),
    key: "Ty4f_NOFO60'",
  },
  {
    name: 'Teymi Townsend',
    age: 30,
    photo: require('../assets/teymi-townsend-AvLHH8qYbAI-unsplash.jpg'),
    key: "AvLHH8qYbAI'",
  },
  {
    name: 'Caique Silva',
    age: 20,
    photo: require('../assets/caique-silva-3ujVzg9i2EI-unsplash.jpg'),
    key: "3ujVzg9i2EI'",
  },
  {
    name: 'David Yanutenama',
    age: 21,
    photo: require('../assets/david-yanutama-5AoO7dBurMw-unsplash.jpg'),
    key: "5AoO7dBurMw'",
  },
]
export default photoCards
Enter fullscreen mode Exit fullscreen mode

Finally, the Swiper component

Once we have the array with card data available to use, we can actually use the Swiper component.

First, we import the necessary elements and initialize the App function. Then, we use a useRef Hook, part of the new and awesome React Hooks API. We need this in order to reference the Swiper component imperatively by pressing one of the handles functions.

import React, { useRef } from 'react'
import { View, Text } from 'react-native'
import Swiper from 'react-native-deck-swiper'
import { photoCards } from './constants'
import { Card, IconButton, OverlayLabel } from './components'
import styles from './App.styles'
const App = () => {
  const useSwiper = useRef(null).current
  const handleOnSwipedLeft = () => useSwiper.swipeLeft()
  const handleOnSwipedTop = () => useSwiper.swipeTop()
  const handleOnSwipedRight = () => useSwiper.swipeRight()
Enter fullscreen mode Exit fullscreen mode

When using the useRef Hook, be sure that the function calling on the actual ref (e.g., here, useSwiper.swipeLeft()) is wrapped in a previously declared function (e.g., here, handleOnSwipedLeft) in order to avoid an error on calling a null object.

Next, inside a return function, we render the Swiper component with the ref set to the useSwiper Hook. Inside the cards prop, we insert the photoCards data array we created earlier and render a single item with a renderCard prop, passing a single item to a Card component.

Inside the overlayLabels prop, there are objects to show the LIKE and NOPE labels while we’re swiping left or right. Those are shown with opacity animation — the closer to the edge, the more visible they are.

return (
  <Swiper
    ref={useSwiper}
    animateCardOpacity
    containerStyle={styles.container}
    cards={photoCards}
    renderCard={card => <Card card={card} />}
    cardIndex={0}
    backgroundColor="white"
    stackSize={2}
    infinite
    showSecondCard
    animateOverlayLabelsOpacity
    overlayLabels={{
      left: {
        title: 'NOPE',
        element: <OverlayLabel label="NOPE" color="#E5566D" />,
        style: {
          wrapper: styles.overlayWrapper,
        },
      },
      right: {
        title: 'LIKE',
        element: <OverlayLabel label="LIKE" color="#4CCC93" />,
        style: {
          wrapper: {
            ...styles.overlayWrapper,
            alignItems: 'flex-start',
            marginLeft: 30,
          },
        },
      },
    }}
  />
Enter fullscreen mode Exit fullscreen mode

In the last section of the App.js component, we render the three buttons for handling the swipe gestures imperatively. By passing name props to the IconButton component, we’re using the awesome react-native-vector-icons library to render nice-looking SVG icons.

  <View style={styles.buttonsContainer}>
    <IconButton
      name="close"
      onPress={handleOnSwipedLeft}
      color="white"
      backgroundColor="#E5566D"
    />
    <IconButton
      name="star"
      onPress={handleOnSwipedTop}
      color="white"
      backgroundColor="#3CA3FF"
    />
    <IconButton
      name="heart"
      onPress={handleOnSwipedRight}
      color="white"
      backgroundColor="#4CCC93"
    />
  </View>
Enter fullscreen mode Exit fullscreen mode

Summary

And here’s how the end result looks:

Alt Text

You can find the full code for this tutorial in my GitHub. The usage of this react-native-deck-swiper component is really smooth and — it definitely helps us save a lot of time. Also, if we tried to implement it from scratch, we’d most likely use the same react-native-gesture-handler API that library author used. That’s why I really recommend using it. I hope that you’ll learn something from this article!


Editor's note: Seeing something wrong with this post? You can find the correct version here.

Plug: LogRocket, a DVR for web apps

 
LogRocket Dashboard Free Trial Banner
 
LogRocket is a frontend logging tool that lets you replay problems as if they happened in your own browser. Instead of guessing why errors happen, or asking users for screenshots and log dumps, LogRocket lets you replay the session to quickly understand what went wrong. It works perfectly with any app, regardless of framework, and has plugins to log additional context from Redux, Vuex, and @ngrx/store.
 
In addition to logging Redux actions and state, LogRocket records console logs, JavaScript errors, stacktraces, network requests/responses with headers + bodies, browser metadata, and custom logs. It also instruments the DOM to record the HTML and CSS on the page, recreating pixel-perfect videos of even the most complex single-page apps.
 
Try it for free.


The post How to make Tinder-like card animations with React Native appeared first on LogRocket Blog.

Latest comments (0)