Skip to content

Commit eb12e6e

Browse files
committed
add app state refresh cookbook
1 parent e653063 commit eb12e6e

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

packages/react-native-sdk/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,43 @@ import { ReflagProvider, useFlag } from "@reflag/react-native-sdk";
2727
<MyApp />
2828
</ReflagProvider>;
2929
```
30+
31+
## Cookbook
32+
33+
### Refresh flags when the app returns to the foreground
34+
35+
```tsx
36+
import React, { useEffect, useRef } from "react";
37+
import { AppState } from "react-native";
38+
import { ReflagProvider, useClient } from "@reflag/react-native-sdk";
39+
40+
function AppStateListener() {
41+
const client = useClient();
42+
const appState = useRef(AppState.currentState);
43+
44+
useEffect(() => {
45+
const subscription = AppState.addEventListener("change", (nextAppState) => {
46+
if (
47+
appState.current.match(/inactive|background/) &&
48+
nextAppState === "active"
49+
) {
50+
void client.refresh();
51+
}
52+
appState.current = nextAppState;
53+
});
54+
55+
return () => subscription.remove();
56+
}, [client]);
57+
58+
return null;
59+
}
60+
61+
export function App() {
62+
return (
63+
<ReflagProvider publishableKey="{YOUR_PUBLISHABLE_KEY}">
64+
<AppStateListener />
65+
<MyApp />
66+
</ReflagProvider>
67+
);
68+
}
69+
```

0 commit comments

Comments
 (0)