This example shows a green box with text that smoothly appears by fading in over 2 seconds.
import React, { useRef, useEffect } from 'react';
import { Animated, View, Text, StyleSheet } from 'react-native';
export default function FadeInExample() {
const fadeAnim = useRef(new Animated.Value(0)).current;
useEffect(() => {
Animated.timing(fadeAnim, {
toValue: 1,
duration: 2000,
useNativeDriver: true
}).start();
}, [fadeAnim]);
return (
<Animated.View style={[styles.box, { opacity: fadeAnim }]}>
<Text style={styles.text}>Hello, I fade in!</Text>
</Animated.View>
);
}
const styles = StyleSheet.create({
box: {
marginTop: 50,
padding: 20,
backgroundColor: '#4CAF50',
borderRadius: 10,
alignItems: 'center'
},
text: {
color: 'white',
fontSize: 18
}
});