Skip to content

Commit

Permalink
Migrate rn-tester/js/examples/AppState/AppStateExample.js to function…
Browse files Browse the repository at this point in the history
… components (facebook#48644)

Summary:

As per title.

Changelog: [Internal]

Reviewed By: christophpurrer

Differential Revision: D68095480
  • Loading branch information
fabriziocucci authored and facebook-github-bot committed Jan 13, 2025
1 parent 4148654 commit 725d99f
Showing 1 changed file with 58 additions and 77 deletions.
135 changes: 58 additions & 77 deletions packages/rn-tester/js/examples/AppState/AppStateExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>,
memoryWarnings: number,
previousAppStates: Array<?(any | string)>,
} = {
appState: AppState.currentState,
previousAppStates: [],
memoryWarnings: 0,
eventsDetected: [],
};
type Props = {
detectEvents?: boolean,
showCurrentOnly?: boolean,
showMemoryWarnings?: boolean,
};

_subscriptions: ?Array<EventSubscription>;
function AppStateSubscription(props: Props) {
const [currentAppState, setCurrentAppState] = useState<?string>(
AppState.currentState,
);
const [previousAppStates, setPreviousAppStates] = useState<string[]>([]);
const [memoryWarnings, setMemoryWarnings] = useState<number>(0);
const [eventsDetected, setEventsDetected] = useState<string[]>([]);

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 (
<View>
<RNTesterText>{this.state.memoryWarnings}</RNTesterText>
</View>
);
}
if (this.props.showCurrentOnly) {
return (
<View>
<RNTesterText>{this.state.appState}</RNTesterText>
</View>
);
}
if (this.props.detectEvents) {
return (
<View>
<RNTesterText>
{JSON.stringify(this.state.eventsDetected)}
</RNTesterText>
</View>
);
}
if (props.showMemoryWarnings) {
return (
<View>
<RNTesterText>{memoryWarnings}</RNTesterText>
</View>
);
}

if (props.showCurrentOnly) {
return (
<View>
<RNTesterText>{currentAppState}</RNTesterText>
</View>
);
}

if (props.detectEvents) {
return (
<View>
<RNTesterText>
{JSON.stringify(this.state.previousAppStates)}
</RNTesterText>
<RNTesterText>{JSON.stringify(eventsDetected)}</RNTesterText>
</View>
);
}

return (
<View>
<RNTesterText>{JSON.stringify(previousAppStates)}</RNTesterText>
</View>
);
}

exports.title = 'AppState';
Expand Down

0 comments on commit 725d99f

Please sign in to comment.