Skip to content

Commit 90d2f65

Browse files
fabriziocuccifacebook-github-bot
authored andcommitted
Migrate rn-tester/js/examples/AppState/AppStateExample.js to function components (#48644)
Summary: Pull Request resolved: #48644 As per title. Changelog: [Internal] Reviewed By: christophpurrer Differential Revision: D68095480 fbshipit-source-id: 2487fa5e66d672883cbfde0c68348a90db09d484
1 parent 05e8007 commit 90d2f65

File tree

1 file changed

+58
-77
lines changed

1 file changed

+58
-77
lines changed

packages/rn-tester/js/examples/AppState/AppStateExample.js

+58-77
Original file line numberDiff line numberDiff line change
@@ -11,109 +11,90 @@
1111
'use strict';
1212

1313
import type {RNTesterModuleExample} from '../../types/RNTesterTypes';
14-
import type {AppStateValues} from 'react-native/Libraries/AppState/AppState';
15-
import type {EventSubscription} from 'react-native/Libraries/vendor/emitter/EventEmitter';
1614

1715
import RNTesterText from '../../components/RNTesterText';
1816
import React from 'react';
17+
import {useEffect, useState} from 'react';
1918
import {AppState, Platform, View} from 'react-native';
2019

21-
class AppStateSubscription extends React.Component<
22-
$FlowFixMeProps,
23-
$FlowFixMeState,
24-
> {
25-
state: {
26-
appState: ?string,
27-
eventsDetected: Array<string>,
28-
memoryWarnings: number,
29-
previousAppStates: Array<?(any | string)>,
30-
} = {
31-
appState: AppState.currentState,
32-
previousAppStates: [],
33-
memoryWarnings: 0,
34-
eventsDetected: [],
35-
};
20+
type Props = {
21+
detectEvents?: boolean,
22+
showCurrentOnly?: boolean,
23+
showMemoryWarnings?: boolean,
24+
};
3625

37-
_subscriptions: ?Array<EventSubscription>;
26+
function AppStateSubscription(props: Props) {
27+
const [currentAppState, setCurrentAppState] = useState<?string>(
28+
AppState.currentState,
29+
);
30+
const [previousAppStates, setPreviousAppStates] = useState<string[]>([]);
31+
const [memoryWarnings, setMemoryWarnings] = useState<number>(0);
32+
const [eventsDetected, setEventsDetected] = useState<string[]>([]);
3833

39-
componentDidMount() {
40-
this._subscriptions = [
41-
AppState.addEventListener('change', this._handleAppStateChange),
42-
AppState.addEventListener('memoryWarning', this._handleMemoryWarning),
34+
useEffect(() => {
35+
const subscriptions = [
36+
AppState.addEventListener('change', handleAppStateChange),
37+
AppState.addEventListener('memoryWarning', handleMemoryWarning),
4338
];
39+
4440
if (Platform.OS === 'android') {
45-
this._subscriptions.push(
46-
AppState.addEventListener('focus', this._handleFocus),
47-
AppState.addEventListener('blur', this._handleBlur),
41+
subscriptions.push(
42+
AppState.addEventListener('focus', handleFocus),
43+
AppState.addEventListener('blur', handleBlur),
4844
);
4945
}
50-
}
5146

52-
componentWillUnmount() {
53-
if (this._subscriptions != null) {
54-
for (const subscription of this._subscriptions) {
55-
subscription.remove();
56-
}
57-
}
58-
}
47+
return () => {
48+
subscriptions.forEach(subscription => subscription.remove());
49+
};
50+
}, []);
5951

60-
_handleMemoryWarning = () => {
61-
this.setState({memoryWarnings: this.state.memoryWarnings + 1});
52+
const handleMemoryWarning = () => {
53+
setMemoryWarnings(prev => prev + 1);
6254
};
6355

64-
_handleBlur = () => {
65-
const eventsDetected = this.state.eventsDetected.slice();
66-
eventsDetected.push('blur');
67-
this.setState({eventsDetected});
56+
const handleBlur = () => {
57+
setEventsDetected(prev => [...prev, 'blur']);
6858
};
6959

70-
_handleFocus = () => {
71-
const eventsDetected = this.state.eventsDetected.slice();
72-
eventsDetected.push('focus');
73-
this.setState({eventsDetected});
60+
const handleFocus = () => {
61+
setEventsDetected(prev => [...prev, 'focus']);
7462
};
7563

76-
_handleAppStateChange = (appState: AppStateValues) => {
77-
const previousAppStates = this.state.previousAppStates.slice();
78-
previousAppStates.push(this.state.appState);
79-
this.setState({
80-
appState,
81-
previousAppStates,
82-
});
64+
const handleAppStateChange = (appState: string) => {
65+
setPreviousAppStates(prev => [...prev, appState]);
66+
setCurrentAppState(appState);
8367
};
8468

85-
render(): React.Node {
86-
if (this.props.showMemoryWarnings) {
87-
return (
88-
<View>
89-
<RNTesterText>{this.state.memoryWarnings}</RNTesterText>
90-
</View>
91-
);
92-
}
93-
if (this.props.showCurrentOnly) {
94-
return (
95-
<View>
96-
<RNTesterText>{this.state.appState}</RNTesterText>
97-
</View>
98-
);
99-
}
100-
if (this.props.detectEvents) {
101-
return (
102-
<View>
103-
<RNTesterText>
104-
{JSON.stringify(this.state.eventsDetected)}
105-
</RNTesterText>
106-
</View>
107-
);
108-
}
69+
if (props.showMemoryWarnings) {
70+
return (
71+
<View>
72+
<RNTesterText>{memoryWarnings}</RNTesterText>
73+
</View>
74+
);
75+
}
76+
77+
if (props.showCurrentOnly) {
78+
return (
79+
<View>
80+
<RNTesterText>{currentAppState}</RNTesterText>
81+
</View>
82+
);
83+
}
84+
85+
if (props.detectEvents) {
10986
return (
11087
<View>
111-
<RNTesterText>
112-
{JSON.stringify(this.state.previousAppStates)}
113-
</RNTesterText>
88+
<RNTesterText>{JSON.stringify(eventsDetected)}</RNTesterText>
11489
</View>
11590
);
11691
}
92+
93+
return (
94+
<View>
95+
<RNTesterText>{JSON.stringify(previousAppStates)}</RNTesterText>
96+
</View>
97+
);
11798
}
11899

119100
exports.title = 'AppState';

0 commit comments

Comments
 (0)