How to Create Fade Animations in React Native?

Nov 11, 2022 . Admin

This article will give you example of fade in animation react native. you will learn how to create animation in react native. I would like to share with you fade in react native. I explained simply step by step react native fade in fade out animation with code examples.

In this example, We will create to fade animation in react native.you can easy nad simply fade animation in react native app.

Step 1: Download Project

In the first step run the following command to create a project.

expo init ExampleApp
Step 2: App.js

In this step, You will open the App.js file and put the code.

import React, {useEffect} from 'react';
import {View, Animated, StyleSheet} from 'react-native';

const FadeIn = () => {
  const startValue = new Animated.Value(0);
  const endValue = 1;
  const duration = 5000;

  useEffect(() => {
    Animated.timing(startValue, {
      toValue: endValue,
      duration: duration,
      useNativeDriver: true,
    }).start();
  }, [duration, endValue, startValue]);

  return (
    <View style={styles.container}>
      <Animated.View style={[styles.square, {opacity: startValue}]} />
    </View>
  );
};
const styles = StyleSheet.create({
  container: {
    justifyContent: 'center',
    alignItems: 'center',
    flex: 1,
  },
  square: {
    height: 200,
    width: 200,
    backgroundColor: 'green',
  },
});

export default FadeIn;
Step 3: Run Project

In the last step run your project using the below command.

expo start

You can QR code scan in Expo Go Application on mobile.

Output:

It will help you...

#React Native