From 98f565c3172a49a7cd8acd871a3543d73f075d22 Mon Sep 17 00:00:00 2001 From: Luna Wei Date: Tue, 23 Mar 2021 18:36:46 -0700 Subject: [PATCH] Move out FadeInView Summary: Changelog: [Internal][Changed] - Move FadeInView example to separate module in RNTester and refactor to use functional component Reviewed By: nadiia Differential Revision: D27245473 fbshipit-source-id: bd9aacc17203dadcee134eee1bae54a5daa16cde --- .../js/examples/Animated/AnimatedExample.js | 76 +---------------- .../js/examples/Animated/FadeInViewExample.js | 82 +++++++++++++++++++ 2 files changed, 84 insertions(+), 74 deletions(-) create mode 100644 packages/rn-tester/js/examples/Animated/FadeInViewExample.js diff --git a/packages/rn-tester/js/examples/Animated/AnimatedExample.js b/packages/rn-tester/js/examples/Animated/AnimatedExample.js index 1ec8649ed21..6c0d2c3b63b 100644 --- a/packages/rn-tester/js/examples/Animated/AnimatedExample.js +++ b/packages/rn-tester/js/examples/Animated/AnimatedExample.js @@ -16,6 +16,7 @@ const {Animated, Easing, StyleSheet, Text, View} = require('react-native'); import type {RNTesterExampleModuleItem} from '../../types/RNTesterTypes'; import RotatingImagesExample from './RotatingImagesExample'; +import FadeInViewExample from './FadeInViewExample'; const styles = StyleSheet.create({ content: { @@ -42,80 +43,7 @@ exports.description = ('Animated provides a powerful ' + 'interactive user experiences.': string); exports.examples = ([ - { - title: 'FadeInView', - name: 'fadeInView', - description: ('Uses a simple timing animation to ' + - 'bring opacity from 0 to 1 when the component ' + - 'mounts.': string), - render: function(): React.Node { - class FadeInView extends React.Component<$FlowFixMeProps, any> { - constructor(props) { - super(props); - this.state = { - fadeAnim: new Animated.Value(0), // opacity 0 - }; - } - componentDidMount() { - Animated.timing( - // Uses easing functions - this.state.fadeAnim, // The value to drive - { - // Target - toValue: 1, - - // Configuration - duration: 2000, - - useNativeDriver: false, - }, - ).start(); // Don't forget start! - } - render() { - return ( - - {this.props.children} - - ); - } - } - - type Props = $ReadOnly<{||}>; - type State = {|show: boolean|}; - class FadeInExample extends React.Component { - constructor(props: Props) { - super(props); - this.state = { - show: true, - }; - } - render() { - return ( - - { - this.setState(state => ({show: !state.show})); - }}> - Press to {this.state.show ? 'Hide' : 'Show'} - - {this.state.show && ( - - - FadeInView - - - )} - - ); - } - } - return ; - }, - }, + FadeInViewExample, { title: 'Transform Bounce', description: ('One `Animated.Value` is driven by a ' + diff --git a/packages/rn-tester/js/examples/Animated/FadeInViewExample.js b/packages/rn-tester/js/examples/Animated/FadeInViewExample.js new file mode 100644 index 00000000000..38df5c33700 --- /dev/null +++ b/packages/rn-tester/js/examples/Animated/FadeInViewExample.js @@ -0,0 +1,82 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow strict-local + * @format + */ + +import type {RNTesterExampleModuleItem} from '../../types/RNTesterTypes'; +import * as React from 'react'; +import RNTesterButton from '../../components/RNTesterButton'; +import {Text, StyleSheet, View, Animated} from 'react-native'; + +const styles = StyleSheet.create({ + content: { + backgroundColor: 'deepskyblue', + borderWidth: 1, + borderColor: 'dodgerblue', + padding: 20, + margin: 20, + borderRadius: 10, + alignItems: 'center', + }, +}); + +function FadeInView({children}: {children: React.Node}) { + //opacity 0 + const [fadeAnim, setFadeAnim] = React.useState(() => new Animated.Value(0)); + React.useEffect(() => { + Animated.timing( + // Uses easing functions + fadeAnim, // The value to drive + { + // Target + toValue: 1, + + // Configuration + duration: 2000, + + useNativeDriver: false, + }, + ).start(); // Don't forget start! + }, [fadeAnim]); + + return ( + + {children} + + ); +} + +function FadeInExample(): React.Node { + const [show, setShow] = React.useState(true); + return ( + + setShow(!show)}> + Press to {show ? 'Hide' : 'Show'} + + {show && ( + + + FadeInView + + + )} + + ); +} + +export default ({ + title: 'FadeInView', + name: 'fadeInView', + description: ('Uses a simple timing animation to ' + + 'bring opacity from 0 to 1 when the component ' + + 'mounts.': string), + render: (): React.Node => , +}: RNTesterExampleModuleItem);