From ba61267015567bf180dd3272a295dc262b3e2c97 Mon Sep 17 00:00:00 2001 From: Nadiia D Date: Thu, 11 Mar 2021 16:40:42 -0800 Subject: [PATCH] Remove deprecated lifecycles usage Summary: Changelog: [General][Changed] createAnimatedComponent: removed deprecated lifecycles usage Reviewed By: lunaleaps Differential Revision: D26734209 fbshipit-source-id: 04d016b30ae0d989890a4b3d8602d47a399dcd11 --- Libraries/Animated/createAnimatedComponent.js | 27 ++++++----- .../js/examples/Animated/AnimatedExample.js | 45 ++++++++++++++++--- 2 files changed, 52 insertions(+), 20 deletions(-) diff --git a/Libraries/Animated/createAnimatedComponent.js b/Libraries/Animated/createAnimatedComponent.js index fda18f0af80744..f7dd9bb4078888 100644 --- a/Libraries/Animated/createAnimatedComponent.js +++ b/Libraries/Animated/createAnimatedComponent.js @@ -53,8 +53,16 @@ function createAnimatedComponent( ); class AnimatedComponent extends React.Component { + constructor(props) { + super(props); + this._waitForUpdate(); + this._attachProps(this.props); + this._lastProps = this.props; + } + _component: any; // TODO T53738161: flow type this, and the whole file _invokeAnimatedPropsCallbackOnMount: boolean = false; + _lastProps: Object; _prevComponent: any; _propsAnimated: AnimatedProps; _eventDetachers: Array = []; @@ -174,10 +182,6 @@ function createAnimatedComponent( _attachProps(nextProps) { const oldPropsAnimated = this._propsAnimated; - if (nextProps === oldPropsAnimated) { - return; - } - this._propsAnimated = new AnimatedProps( nextProps, this._animatedPropsCallback, @@ -219,6 +223,11 @@ function createAnimatedComponent( }); render() { + if (this._lastProps !== this.props) { + this._waitForUpdate(); + this._attachProps(this.props); + this._lastProps = this.props; + } const {style = {}, ...props} = this._propsAnimated.__getValue() || {}; const {style: passthruStyle = {}, ...passthruProps} = this.props.passthroughAnimatedPropExplicitValues || {}; @@ -263,11 +272,6 @@ function createAnimatedComponent( ); } - UNSAFE_componentWillMount() { - this._waitForUpdate(); - this._attachProps(this.props); - } - componentDidMount() { if (this._invokeAnimatedPropsCallbackOnMount) { this._invokeAnimatedPropsCallbackOnMount = false; @@ -279,11 +283,6 @@ function createAnimatedComponent( this._markUpdateComplete(); } - UNSAFE_componentWillReceiveProps(newProps) { - this._waitForUpdate(); - this._attachProps(newProps); - } - componentDidUpdate(prevProps) { if (this._component !== this._prevComponent) { this._propsAnimated.setNativeView(this._component); diff --git a/packages/rn-tester/js/examples/Animated/AnimatedExample.js b/packages/rn-tester/js/examples/Animated/AnimatedExample.js index e8e64ac518f2b1..154bc406a4b92e 100644 --- a/packages/rn-tester/js/examples/Animated/AnimatedExample.js +++ b/packages/rn-tester/js/examples/Animated/AnimatedExample.js @@ -12,7 +12,14 @@ const RNTesterButton = require('../../components/RNTesterButton'); const React = require('react'); -const {Animated, Easing, StyleSheet, Text, View} = require('react-native'); +const { + Animated, + Easing, + Pressable, + StyleSheet, + Text, + View, +} = require('react-native'); import type {RNTesterExampleModuleItem} from '../../types/RNTesterTypes'; @@ -83,11 +90,17 @@ exports.examples = ([ } type Props = $ReadOnly<{||}>; - type State = {|show: boolean|}; + type State = {| + disabled: boolean, + pressCount: number, + show: boolean, + |}; class FadeInExample extends React.Component { constructor(props: Props) { super(props); this.state = { + disabled: true, + pressCount: 0, show: true, }; } @@ -97,15 +110,35 @@ exports.examples = ([ { - this.setState(state => ({show: !state.show})); + this.setState(state => ({pressCount: 0, show: !state.show})); }}> Press to {this.state.show ? 'Hide' : 'Show'} + { + this.setState(state => ({disabled: !state.disabled})); + }}> + Press to {this.state.disabled ? 'Enable' : 'Disable'} + {this.state.show && ( - - FadeInView - + { + this.setState(state => ({ + pressCount: this.state.pressCount + 1, + })); + }}> + + FadeInView + {this.state.disabled + ? '' + : ` Pressable: ${this.state.pressCount}`} + + )}