How to Build the Favorite Feature from Spotify
This is a step by step tutorial on how to build the ‘Favorite’ feature from Spotify in React Native using Expo.
This is a problem that I had to solve for a client. No matter where I searched, I could not find a third party component or tutorial on how to do it, so I did it myself, and I want to help you guys do it in the future.
you want to install the React Native, Expo, and the React Native dependencies and libraries I am using. I used npm in this instance, but you can use yarn if you want.
npm install --save react-native-swipe-list-view
npm install --save react-native-responsive-screen
First of all, you want to create a two React component. One, where your Favorite Swiper will live and another for the Favorite Modal to appear in. The Favorite Swiper is a list of items (songs) where they can be swiped left or right (left) and favorited. The Favorite Modal is a pop-up that shows one an item has been favorited. For this tutorial, I called my components SpotifyFavoriteExample.js and FavoriteModal.js. I decided on a class base architecture.
From there, you want to define and initialize the animated values as an array equal to the length of swipe-able items in the list in the constructor of SpotifyFavoriteExample.js.
Notice that I created a data source state that holds an array of 3 placeholder items. These are the items that will be ‘Favorited’.
After that, we will define 3 things. The rendered row, the listener on the rendered row for its ‘swipe’ action and the behavior of the row when it is swiped.
Let’s start with the rendered row. In the <SwipeListView/> component we will define a property called renderItem where we will define how the item for each row will be rendered. From there we will define the threshold of actions for each animation style.
animatedStyle will change the color of grey to the ‘like_color’ which can be either ‘green’ or ‘red’ based on if the user liked it.
iconAminatedStyle will change transform the right margin of the like icon based off it’s current swipe value.
fontAnimatedStyle will determine if the icon should be a full heart or an empty hart. This style will become a full heart when the user has passed the threshold.
backAnimatedStyle is similar to fontAnimatedStyle. It will change the empty heart icon to a full icon.
Once we have defined how each of our animated style should behave as the input ranged change, we will define the <SwipeRow />’s onSwipeValueChange. This is the listener that will determine what should happen at a certain threshold of a row swipe.
We hard set all the animated styles to make a set change at an X swipe value of -140. The only animated style that will not listen to the hard set value of -140 is the heart icon’s marginRight value.
From there, we will need to define when a row is ‘favorited’ with the property value, onRowDidOpen.
We will set this function to trigger when the rowDidOpen function is triggered. This will open the favorite modal to indicate to users that the user has liked the row and also change the row data from unliked to liked or liked to unliked. Then it will close the row.
This is my solution to the Spotify Favorite Feature. If you have a better or more optimized solution to this issue, feel free to hit me up, I’d love to listen to your solution. Other than that, happy coding :).