|
11 | 11 | 'use strict';
|
12 | 12 |
|
13 | 13 | 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'; |
16 | 14 |
|
17 | 15 | import RNTesterText from '../../components/RNTesterText';
|
18 | 16 | import React from 'react';
|
| 17 | +import {useEffect, useState} from 'react'; |
19 | 18 | import {AppState, Platform, View} from 'react-native';
|
20 | 19 |
|
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 | +}; |
36 | 25 |
|
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[]>([]); |
38 | 33 |
|
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), |
43 | 38 | ];
|
| 39 | + |
44 | 40 | 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), |
48 | 44 | );
|
49 | 45 | }
|
50 |
| - } |
51 | 46 |
|
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 | + }, []); |
59 | 51 |
|
60 |
| - _handleMemoryWarning = () => { |
61 |
| - this.setState({memoryWarnings: this.state.memoryWarnings + 1}); |
| 52 | + const handleMemoryWarning = () => { |
| 53 | + setMemoryWarnings(prev => prev + 1); |
62 | 54 | };
|
63 | 55 |
|
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']); |
68 | 58 | };
|
69 | 59 |
|
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']); |
74 | 62 | };
|
75 | 63 |
|
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); |
83 | 67 | };
|
84 | 68 |
|
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) { |
109 | 86 | return (
|
110 | 87 | <View>
|
111 |
| - <RNTesterText> |
112 |
| - {JSON.stringify(this.state.previousAppStates)} |
113 |
| - </RNTesterText> |
| 88 | + <RNTesterText>{JSON.stringify(eventsDetected)}</RNTesterText> |
114 | 89 | </View>
|
115 | 90 | );
|
116 | 91 | }
|
| 92 | + |
| 93 | + return ( |
| 94 | + <View> |
| 95 | + <RNTesterText>{JSON.stringify(previousAppStates)}</RNTesterText> |
| 96 | + </View> |
| 97 | + ); |
117 | 98 | }
|
118 | 99 |
|
119 | 100 | exports.title = 'AppState';
|
|
0 commit comments