Skip to content

Commit 9999f76

Browse files
committed
add app state listener to expo demo
1 parent e26cbf2 commit 9999f76

File tree

1 file changed

+21
-2
lines changed
  • packages/react-native-sdk/dev/expo

1 file changed

+21
-2
lines changed

packages/react-native-sdk/dev/expo/App.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React from "react";
2-
import { Button, StyleSheet, Text, View } from "react-native";
1+
import React, { useEffect, useRef } from "react";
2+
import { AppState, Button, StyleSheet, Text, View } from "react-native";
33
import { SafeAreaProvider, SafeAreaView } from "react-native-safe-area-context";
44

55
import {
@@ -32,6 +32,25 @@ function FlagCard() {
3232
}
3333

3434
export default function App() {
35+
const appState = useRef(AppState.currentState);
36+
37+
useEffect(() => {
38+
const subscription = AppState.addEventListener("change", (nextAppState) => {
39+
if (
40+
appState.current.match(/inactive|background/) &&
41+
nextAppState === "active"
42+
) {
43+
console.log("App came to foreground");
44+
// Your global logic here
45+
}
46+
appState.current = nextAppState;
47+
});
48+
49+
return () => {
50+
subscription.remove();
51+
};
52+
}, []);
53+
3554
return (
3655
<SafeAreaProvider>
3756
<SafeAreaView style={styles.container}>

0 commit comments

Comments
 (0)