Skip to content

Instantly share code, notes, and snippets.

@vcapretz
Created January 15, 2021 14:00
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vcapretz/e7b172154774e24487d3c46677b08de2 to your computer and use it in GitHub Desktop.
Save vcapretz/e7b172154774e24487d3c46677b08de2 to your computer and use it in GitHub Desktop.
A simple Like button with a nice animation using react-native-reanimated v2, support for my post in https://vcapretz.com/2021/instagram-button-react-native
import React from "react";
import Animated, {
useSharedValue,
withSpring,
useAnimatedStyle,
Extrapolate,
interpolate,
} from "react-native-reanimated";
import { Pressable, View, Button, StyleSheet } from "react-native";
import { MaterialCommunityIcons } from "@expo/vector-icons";
const LikeButton = () => {
const liked = useSharedValue(0);
const outlineStyle = useAnimatedStyle(() => {
return {
transform: [
{
scale: interpolate(liked.value, [0, 1], [1, 0], Extrapolate.CLAMP),
},
],
};
});
const fillStyle = useAnimatedStyle(() => {
return {
transform: [
{
scale: liked.value,
},
],
opacity: liked.value,
};
});
return (
<Pressable onPress={() => (liked.value = withSpring(liked.value ? 0 : 1))}>
<Animated.View style={[StyleSheet.absoluteFillObject, outlineStyle]}>
<MaterialCommunityIcons
name={"heart-outline"}
size={32}
color={"black"}
/>
</Animated.View>
<Animated.View style={fillStyle}>
<MaterialCommunityIcons name={"heart"} size={32} color={"red"} />
</Animated.View>
</Pressable>
);
};
export default function AnimatedStyleUpdateExample(props) {
return (
<View
style={{
flex: 1,
alignItems: "center",
justifyContent: "center",
flexDirection: "column",
}}
>
<LikeButton />
</View>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment