|
1 | 1 | import { FeaturevisorProvider } from "@featurevisor/react"; |
2 | | -import { createInstance } from "@featurevisor/sdk"; |
| 2 | +import { createFeaturevisor } from "@featurevisor/sdk"; |
| 3 | +import { useFonts } from "expo-font"; |
| 4 | +import { Stack } from "expo-router"; |
3 | 5 | import { |
4 | 6 | DarkTheme, |
5 | 7 | DefaultTheme, |
6 | 8 | ThemeProvider, |
7 | | -} from "@react-navigation/native"; |
8 | | -import { useFonts } from "expo-font"; |
9 | | -import { Stack } from "expo-router"; |
| 9 | +} from "expo-router/react-navigation"; |
10 | 10 | import { StatusBar } from "expo-status-bar"; |
| 11 | +import { useEffect, useState } from "react"; |
11 | 12 | import "react-native-reanimated"; |
12 | 13 |
|
13 | 14 | import { useColorScheme } from "@/hooks/useColorScheme"; |
14 | 15 |
|
15 | 16 | const DATAFILE_URL = |
16 | 17 | "https://featurevisor-example-cloudflare.pages.dev/production/featurevisor-tag-all.json"; |
17 | 18 |
|
18 | | -const f = createInstance({ |
| 19 | +const f = createFeaturevisor({ |
19 | 20 | context: { userId: "123" }, |
20 | 21 | }); |
21 | 22 |
|
22 | | -fetch(DATAFILE_URL) |
23 | | - .then((res) => res.json()) |
24 | | - .then((datafileContent) => f.setDatafile(datafileContent)); |
25 | | - |
26 | 23 | export default function RootLayout() { |
27 | 24 | const colorScheme = useColorScheme(); |
| 25 | + const [datafileLoaded, setDatafileLoaded] = useState(false); |
28 | 26 | const [loaded] = useFonts({ |
29 | 27 | SpaceMono: require("../assets/fonts/SpaceMono-Regular.ttf"), |
30 | 28 | }); |
31 | 29 |
|
32 | | - if (!loaded) { |
33 | | - // Async font loading only occurs in development. |
| 30 | + useEffect(() => { |
| 31 | + let active = true; |
| 32 | + |
| 33 | + fetch(DATAFILE_URL) |
| 34 | + .then((response) => { |
| 35 | + if (!response.ok) { |
| 36 | + throw new Error(`Request failed with status ${response.status}`); |
| 37 | + } |
| 38 | + |
| 39 | + return response.json(); |
| 40 | + }) |
| 41 | + .then((datafileContent) => { |
| 42 | + if (active) { |
| 43 | + f.setDatafile(datafileContent, true); |
| 44 | + } |
| 45 | + }) |
| 46 | + .catch((error) => { |
| 47 | + console.error("Could not load Featurevisor datafile:", error); |
| 48 | + }) |
| 49 | + .finally(() => { |
| 50 | + if (active) { |
| 51 | + setDatafileLoaded(true); |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + return () => { |
| 56 | + active = false; |
| 57 | + }; |
| 58 | + }, []); |
| 59 | + |
| 60 | + if (!loaded || !datafileLoaded) { |
34 | 61 | return null; |
35 | 62 | } |
36 | 63 |
|
|
0 commit comments