Skip to content

Commit e653063

Browse files
committed
refresh flags on app foreground
1 parent 9999f76 commit e653063

File tree

1 file changed

+25
-19
lines changed
  • packages/react-native-sdk/dev/expo

1 file changed

+25
-19
lines changed

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

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,6 @@ 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-
5435
return (
5536
<SafeAreaProvider>
5637
<SafeAreaView style={styles.container}>
@@ -71,13 +52,38 @@ export default function App() {
7152
: "Set EXPO_PUBLIC_REFLAG_PUBLISHABLE_KEY to fetch real flags"}
7253
</Text>
7354
</View>
55+
<AppStateListener />
7456
<FlagCard />
7557
</ReflagProvider>
7658
</SafeAreaView>
7759
</SafeAreaProvider>
7860
);
7961
}
8062

63+
function AppStateListener() {
64+
const client = useClient();
65+
const appState = useRef(AppState.currentState);
66+
67+
useEffect(() => {
68+
const subscription = AppState.addEventListener("change", (nextAppState) => {
69+
if (
70+
appState.current.match(/inactive|background/) &&
71+
nextAppState === "active"
72+
) {
73+
console.log("App came to foreground");
74+
void client.refresh();
75+
}
76+
appState.current = nextAppState;
77+
});
78+
79+
return () => {
80+
subscription.remove();
81+
};
82+
}, [client]);
83+
84+
return null;
85+
}
86+
8187
const styles = StyleSheet.create({
8288
container: {
8389
flex: 1,

0 commit comments

Comments
 (0)