Last active
April 14, 2024 07:18
-
-
Save arnoldc/c06471c4c1bd9617de7cd163750ec5ca to your computer and use it in GitHub Desktop.
[React Native] Reanimated.tips
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TIP 1 | |
// - Using the useSharedValue hook | |
// - Using the useAnimatedStyle hook | |
const AnimatedComponents = () => { | |
const buttonPressed = () => { | |
// Toggle the animation state based on button press | |
isAnimating.value = !isAnimating.value; | |
}; | |
// Shared values for animation | |
const isAnimating = useSharedValue(false); | |
const animationDuration = 500; | |
// Animated styles for the first component | |
const box1Style = useAnimatedStyle(() => { | |
return { | |
width: isAnimating.value ? withTiming(200, { duration: animationDuration }) : withTiming(100, { duration: animationDuration }), | |
height: isAnimating.value ? withTiming(200, { duration: animationDuration }) : withTiming(100, { duration: animationDuration }), | |
backgroundColor: isAnimating.value ? 'blue' : 'green', | |
}; | |
}); | |
// Animated styles for the second component | |
const box2Style = useAnimatedStyle(() => { | |
return { | |
transform: [ | |
{ translateY: isAnimating.value ? withTiming(-50, { duration: animationDuration }) : withTiming(0, { duration: animationDuration }) }, | |
], | |
}; | |
}); | |
return ( | |
<View style={styles.container}> | |
<Animated.View style={[styles.box1, box1Style]} /> | |
<Animated.View style={[styles.box2, box2Style]} /> | |
<TouchableOpacity onPress={buttonPressed} style={styles.button}> | |
<Text style={styles.buttonText}>Animate</Text> | |
</TouchableOpacity> | |
</View> | |
); | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment