File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
packages/react-native-sdk Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff 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+ ```
You can’t perform that action at this time.
0 commit comments