diff --git a/docs/animated.md b/docs/animated.md index 434efe5984f..f685e5d4671 100644 --- a/docs/animated.md +++ b/docs/animated.md @@ -3,20 +3,194 @@ id: animated title: Animated --- -The `Animated` library is designed to make animations fluid, powerful, and painless to build and maintain. `Animated` focuses on declarative relationships between inputs and outputs, with configurable transforms in between, and `start`/`stop` methods to control time-based animation execution. - -The most basic workflow for creating an animation is to create an `Animated.Value`, hook it up to one or more style attributes of an animated component, and then drive updates via animations using `Animated.timing()`: +The `Animated` library is designed to make animations fluid, powerful, and painless to build and maintain. `Animated` focuses on declarative relationships between inputs and outputs, configurable transforms in between, and `start`/`stop` methods to control time-based animation execution. + +The core workflow for creating an animation is to create an `Animated.Value`, hook it up to one or more style attributes of an animated component, and then drive updates via animations using `Animated.timing()`. + + +
+ +
+ + + +> Don't modify the animated value directly. You can use the [`useRef` Hook](https://reactjs.org/docs/hooks-reference.html#useref) to return a mutable ref object. This ref object's `current` property is initialized as the given argument and persists throughout the component lifecycle. + + + +> Don't modify the animated value directly. It is usually stored as a [state variable](intro-react#state) in class components. + + + + +## Example + +The following example contains a `View` which will fade in and fade out based on the animated value `fadeAnim` + +
+
    + + +
+
+ + + +```SnackPlayer name=Animated +import React, { useRef } from "react"; +import { Animated, Text, View, StyleSheet, Button } from "react-native"; + +export default function App() { + // fadeAnim will be used as the value for opacity. Initial Value: 0 + const fadeAnim = useRef(new Animated.Value(0)).current; + + const fadeIn = () => { + // Will change fadeAnim value to 1 in 5 seconds + Animated.timing(fadeAnim, { + toValue: 1, + duration: 2000 + }).start(); + }; + + const fadeOut = () => { + // Will change fadeAnim value to 0 in 5 seconds + Animated.timing(fadeAnim, { + toValue: 0, + duration: 2000 + }).start(); + }; + + return ( + + + Fading View! + + +