Animated API
Basic Animation
javascript
import { Animated } from 'react-native';
function FadeIn() {
const fadeAnim = useRef(new Animated.Value(0)).current;
useEffect(() => {
Animated.timing(fadeAnim, {
toValue: 1,
duration: 1000,
useNativeDriver: true,
}).start();
}, []);
return (
<Animated.View style={{ opacity: fadeAnim }}>
<Text>Fades in</Text>
</Animated.View>
);
}Interpolation
javascript
const translateAnim = useRef(new Animated.Value(0)).current;
const rotate = translateAnim.interpolate({
inputRange: [0, 1],
outputRange: ['0deg', '360deg'],
});
<Animated.View
style={{
transform: [{ rotate }],
}}
>
<Text>Rotates</Text>
</Animated.View>Parallel Animations
javascript
const fadeAnim = useRef(new Animated.Value(0)).current;
const scaleAnim = useRef(new Animated.Value(1)).current;
Animated.parallel([
Animated.timing(fadeAnim, {
toValue: 1,
duration: 1000,
useNativeDriver: true,
}),
Animated.spring(scaleAnim, {
toValue: 2,
friction: 3,
useNativeDriver: true,
}),
]).start();Sequence Animations
javascript
Animated.sequence([
Animated.timing(fadeAnim, {
toValue: 1,
duration: 500,
useNativeDriver: true,
}),
Animated.timing(scaleAnim, {
toValue: 1.5,
duration: 500,
useNativeDriver: true,
}),
]).start();Spring Animation
javascript
const springAnim = useRef(new Animated.Value(0)).current;
Animated.spring(springAnim, {
toValue: 1,
friction: 7,
tension: 40,
useNativeDriver: true,
}).start();Gesture Handler
Pan Gesture
javascript
import { PanGestureHandler, State } from 'react-native-gesture-handler';
const translateX = useRef(new Animated.Value(0)).current;
const onGestureEvent = Animated.event(
[{ nativeEvent: { translationX: translateX } }],
{ useNativeDriver: true }
);
<PanGestureHandler
onGestureEvent={onGestureEvent}
onHandlerStateChange={(event) => {
if (event.nativeEvent.state === State.END) {
Animated.spring(translateX, {
toValue: 0,
useNativeDriver: true,
}).start();
}
}}
>
<Animated.View
style={[
styles.box,
{ transform: [{ translateX }] },
]}
/>
</PanGestureHandler>Reanimated
Installation
bash
npm install react-native-reanimatedBasic Usage
javascript
import Animated, {
useSharedValue,
useAnimatedStyle,
withTiming,
withSpring,
} from 'react-native-reanimated';
function Box() {
const offset = useSharedValue(0);
const animatedStyle = useAnimatedStyle(() => ({
transform: [{ translateX: offset.value }],
}));
return (
<>
<Animated.View style={[styles.box, animatedStyle]} />
<Button
title="Move"
onPress={() => {
offset.value = withSpring(offset.value + 50);
}}
/>
</>
);
}Layout Animation
javascript
import { LayoutAnimation } from 'react-native';
function expandItem() {
LayoutAnimation.configureNext(
LayoutAnimation.create(
300,
LayoutAnimation.Types.easeInEaseOut,
LayoutAnimation.Properties.opacity
)
);
setExpanded(!expanded);
}
<View style={{ height: expanded ? 200 : 100 }}>
<Text>Content</Text>
</View>Best Practices
- 1Use native driver: Better performance
- 2Avoid layout animations: Can be janky
- 3Use Reanimated: Complex animations
- 4Test on low-end devices: Ensure smoothness
- 5Keep it simple: Don't over-animate
Performance Tips
- 1Avoid animating layout properties: Use transform/opacity
- 2Use native driver: Offload to native thread
- 3Reduce complexity: Fewer parallel animations
- 4Profile: Use Performance monitor
Conclusion
Animations enhance user experience. Use them thoughtfully to create delightful interactions.