From 479e1088801f7b25b8953d11cb41f0ae6b3aa01a Mon Sep 17 00:00:00 2001 From: Fabrizio Cucci Date: Mon, 13 Jan 2025 08:12:17 -0800 Subject: [PATCH] Migrate rn-tester/js/examples/AppState/AppStateExample.js to function components (#48644) Summary: As per title. Changelog: [Internal] Reviewed By: christophpurrer Differential Revision: D68095480 --- .../js/examples/AppState/AppStateExample.js | 135 ++++++++---------- 1 file changed, 58 insertions(+), 77 deletions(-) diff --git a/packages/rn-tester/js/examples/AppState/AppStateExample.js b/packages/rn-tester/js/examples/AppState/AppStateExample.js index f4e132ba2d4b84..2415cb0c418ccf 100644 --- a/packages/rn-tester/js/examples/AppState/AppStateExample.js +++ b/packages/rn-tester/js/examples/AppState/AppStateExample.js @@ -11,109 +11,90 @@ 'use strict'; import type {RNTesterModuleExample} from '../../types/RNTesterTypes'; -import type {AppStateValues} from 'react-native/Libraries/AppState/AppState'; -import type {EventSubscription} from 'react-native/Libraries/vendor/emitter/EventEmitter'; import RNTesterText from '../../components/RNTesterText'; import React from 'react'; +import {useEffect, useState} from 'react'; import {AppState, Platform, View} from 'react-native'; -class AppStateSubscription extends React.Component< - $FlowFixMeProps, - $FlowFixMeState, -> { - state: { - appState: ?string, - eventsDetected: Array, - memoryWarnings: number, - previousAppStates: Array, - } = { - appState: AppState.currentState, - previousAppStates: [], - memoryWarnings: 0, - eventsDetected: [], - }; +type Props = { + detectEvents?: boolean, + showCurrentOnly?: boolean, + showMemoryWarnings?: boolean, +}; - _subscriptions: ?Array; +function AppStateSubscription(props: Props) { + const [currentAppState, setCurrentAppState] = useState( + AppState.currentState, + ); + const [previousAppStates, setPreviousAppStates] = useState([]); + const [memoryWarnings, setMemoryWarnings] = useState(0); + const [eventsDetected, setEventsDetected] = useState([]); - componentDidMount() { - this._subscriptions = [ - AppState.addEventListener('change', this._handleAppStateChange), - AppState.addEventListener('memoryWarning', this._handleMemoryWarning), + useEffect(() => { + const subscriptions = [ + AppState.addEventListener('change', handleAppStateChange), + AppState.addEventListener('memoryWarning', handleMemoryWarning), ]; + if (Platform.OS === 'android') { - this._subscriptions.push( - AppState.addEventListener('focus', this._handleFocus), - AppState.addEventListener('blur', this._handleBlur), + subscriptions.push( + AppState.addEventListener('focus', handleFocus), + AppState.addEventListener('blur', handleBlur), ); } - } - componentWillUnmount() { - if (this._subscriptions != null) { - for (const subscription of this._subscriptions) { - subscription.remove(); - } - } - } + return () => { + subscriptions.forEach(subscription => subscription.remove()); + }; + }, []); - _handleMemoryWarning = () => { - this.setState({memoryWarnings: this.state.memoryWarnings + 1}); + const handleMemoryWarning = () => { + setMemoryWarnings(prev => prev + 1); }; - _handleBlur = () => { - const eventsDetected = this.state.eventsDetected.slice(); - eventsDetected.push('blur'); - this.setState({eventsDetected}); + const handleBlur = () => { + setEventsDetected(prev => [...prev, 'blur']); }; - _handleFocus = () => { - const eventsDetected = this.state.eventsDetected.slice(); - eventsDetected.push('focus'); - this.setState({eventsDetected}); + const handleFocus = () => { + setEventsDetected(prev => [...prev, 'focus']); }; - _handleAppStateChange = (appState: AppStateValues) => { - const previousAppStates = this.state.previousAppStates.slice(); - previousAppStates.push(this.state.appState); - this.setState({ - appState, - previousAppStates, - }); + const handleAppStateChange = (appState: string) => { + setPreviousAppStates(prev => [...prev, appState]); + setCurrentAppState(appState); }; - render(): React.Node { - if (this.props.showMemoryWarnings) { - return ( - - {this.state.memoryWarnings} - - ); - } - if (this.props.showCurrentOnly) { - return ( - - {this.state.appState} - - ); - } - if (this.props.detectEvents) { - return ( - - - {JSON.stringify(this.state.eventsDetected)} - - - ); - } + if (props.showMemoryWarnings) { + return ( + + {memoryWarnings} + + ); + } + + if (props.showCurrentOnly) { + return ( + + {currentAppState} + + ); + } + + if (props.detectEvents) { return ( - - {JSON.stringify(this.state.previousAppStates)} - + {JSON.stringify(eventsDetected)} ); } + + return ( + + {JSON.stringify(previousAppStates)} + + ); } exports.title = 'AppState';