Skip to content

Commit ba61267

Browse files
Nadiia Dfacebook-github-bot
Nadiia D
authored andcommitted
Remove deprecated lifecycles usage
Summary: Changelog: [General][Changed] createAnimatedComponent: removed deprecated lifecycles usage Reviewed By: lunaleaps Differential Revision: D26734209 fbshipit-source-id: 04d016b30ae0d989890a4b3d8602d47a399dcd11
1 parent b512beb commit ba61267

File tree

2 files changed

+52
-20
lines changed

2 files changed

+52
-20
lines changed

Libraries/Animated/createAnimatedComponent.js

+13-14
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,16 @@ function createAnimatedComponent<Props: {+[string]: mixed, ...}, Instance>(
5353
);
5454

5555
class AnimatedComponent extends React.Component<Object> {
56+
constructor(props) {
57+
super(props);
58+
this._waitForUpdate();
59+
this._attachProps(this.props);
60+
this._lastProps = this.props;
61+
}
62+
5663
_component: any; // TODO T53738161: flow type this, and the whole file
5764
_invokeAnimatedPropsCallbackOnMount: boolean = false;
65+
_lastProps: Object;
5866
_prevComponent: any;
5967
_propsAnimated: AnimatedProps;
6068
_eventDetachers: Array<Function> = [];
@@ -174,10 +182,6 @@ function createAnimatedComponent<Props: {+[string]: mixed, ...}, Instance>(
174182
_attachProps(nextProps) {
175183
const oldPropsAnimated = this._propsAnimated;
176184

177-
if (nextProps === oldPropsAnimated) {
178-
return;
179-
}
180-
181185
this._propsAnimated = new AnimatedProps(
182186
nextProps,
183187
this._animatedPropsCallback,
@@ -219,6 +223,11 @@ function createAnimatedComponent<Props: {+[string]: mixed, ...}, Instance>(
219223
});
220224

221225
render() {
226+
if (this._lastProps !== this.props) {
227+
this._waitForUpdate();
228+
this._attachProps(this.props);
229+
this._lastProps = this.props;
230+
}
222231
const {style = {}, ...props} = this._propsAnimated.__getValue() || {};
223232
const {style: passthruStyle = {}, ...passthruProps} =
224233
this.props.passthroughAnimatedPropExplicitValues || {};
@@ -263,11 +272,6 @@ function createAnimatedComponent<Props: {+[string]: mixed, ...}, Instance>(
263272
);
264273
}
265274

266-
UNSAFE_componentWillMount() {
267-
this._waitForUpdate();
268-
this._attachProps(this.props);
269-
}
270-
271275
componentDidMount() {
272276
if (this._invokeAnimatedPropsCallbackOnMount) {
273277
this._invokeAnimatedPropsCallbackOnMount = false;
@@ -279,11 +283,6 @@ function createAnimatedComponent<Props: {+[string]: mixed, ...}, Instance>(
279283
this._markUpdateComplete();
280284
}
281285

282-
UNSAFE_componentWillReceiveProps(newProps) {
283-
this._waitForUpdate();
284-
this._attachProps(newProps);
285-
}
286-
287286
componentDidUpdate(prevProps) {
288287
if (this._component !== this._prevComponent) {
289288
this._propsAnimated.setNativeView(this._component);

packages/rn-tester/js/examples/Animated/AnimatedExample.js

+39-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@
1212

1313
const RNTesterButton = require('../../components/RNTesterButton');
1414
const React = require('react');
15-
const {Animated, Easing, StyleSheet, Text, View} = require('react-native');
15+
const {
16+
Animated,
17+
Easing,
18+
Pressable,
19+
StyleSheet,
20+
Text,
21+
View,
22+
} = require('react-native');
1623

1724
import type {RNTesterExampleModuleItem} from '../../types/RNTesterTypes';
1825

@@ -83,11 +90,17 @@ exports.examples = ([
8390
}
8491

8592
type Props = $ReadOnly<{||}>;
86-
type State = {|show: boolean|};
93+
type State = {|
94+
disabled: boolean,
95+
pressCount: number,
96+
show: boolean,
97+
|};
8798
class FadeInExample extends React.Component<Props, State> {
8899
constructor(props: Props) {
89100
super(props);
90101
this.state = {
102+
disabled: true,
103+
pressCount: 0,
91104
show: true,
92105
};
93106
}
@@ -97,15 +110,35 @@ exports.examples = ([
97110
<RNTesterButton
98111
testID="toggle-button"
99112
onPress={() => {
100-
this.setState(state => ({show: !state.show}));
113+
this.setState(state => ({pressCount: 0, show: !state.show}));
101114
}}>
102115
Press to {this.state.show ? 'Hide' : 'Show'}
103116
</RNTesterButton>
117+
<RNTesterButton
118+
testID="toggle-pressable"
119+
onPress={() => {
120+
this.setState(state => ({disabled: !state.disabled}));
121+
}}>
122+
Press to {this.state.disabled ? 'Enable' : 'Disable'}
123+
</RNTesterButton>
104124
{this.state.show && (
105125
<FadeInView>
106-
<View testID="fade-in-view" style={styles.content}>
107-
<Text>FadeInView</Text>
108-
</View>
126+
<Pressable
127+
testID="fade-in-view"
128+
style={styles.content}
129+
disabled={this.state.disabled}
130+
onPress={() => {
131+
this.setState(state => ({
132+
pressCount: this.state.pressCount + 1,
133+
}));
134+
}}>
135+
<Text>
136+
FadeInView
137+
{this.state.disabled
138+
? ''
139+
: ` Pressable: ${this.state.pressCount}`}
140+
</Text>
141+
</Pressable>
109142
</FadeInView>
110143
)}
111144
</View>

0 commit comments

Comments
 (0)