Skip to content

Commit 67d2298

Browse files
committed
fix: improve font loading flow
1 parent 3bcf159 commit 67d2298

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

example/app/(app)/_layout.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,19 @@ import 'react-native-reanimated'
33
import { Stack } from 'expo-router'
44

55
import { useListenLocalStorage } from '@/common/localStorage'
6+
import { Spinner } from '@/components/Spinner'
7+
import { useSplashScreen } from '@/hooks/useSplashScreen'
68

79
export default function AppLayout() {
10+
const ready = useSplashScreen()
811
const [accessToken] = useListenLocalStorage('accessToken')
912

1013
const isLoggedIn = Boolean(accessToken)
1114

15+
if (!ready) {
16+
return <Spinner />
17+
}
18+
1219
return (
1320
<Stack screenOptions={{ headerShown: false }}>
1421
<Stack.Protected guard={isLoggedIn}>

example/app/_layout.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { Slot } from 'expo-router'
1010
import { StatusBar } from 'expo-status-bar'
1111
import { useLayoutEffect } from 'react'
1212

13-
import { SplashScreen } from '@/components/SplashScreen'
1413
import { useColorScheme } from '@/hooks/useColorScheme'
1514

1615
export default function RootLayout() {
@@ -22,7 +21,6 @@ export default function RootLayout() {
2221

2322
return (
2423
<ThemeProvider value={colorScheme === 'dark' ? DarkTheme : DefaultTheme}>
25-
<SplashScreen />
2624
<Slot />
2725
<StatusBar style="auto" />
2826
</ThemeProvider>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { ActivityIndicator, ActivityIndicatorProps } from 'react-native'
2+
3+
import { Color } from '@/constants/Colors'
4+
5+
export function Spinner(props: ActivityIndicatorProps) {
6+
return <ActivityIndicator color={Color.Green} {...props} />
7+
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
import 'react-native-reanimated'
2-
31
import { useFonts } from 'expo-font'
42
import * as ExpoSplashScreen from 'expo-splash-screen'
5-
import { FunctionComponent, useEffect } from 'react'
3+
import { useEffect, useState } from 'react'
64

75
import SpaceMono from '@/assets/fonts/SpaceMono-Regular.ttf'
86

97
ExpoSplashScreen.preventAutoHideAsync()
108

11-
export const SplashScreen: FunctionComponent = () => {
9+
export const useSplashScreen = () => {
1210
const [loaded] = useFonts({ SpaceMono })
11+
const [ready, setReady] = useState<boolean>(false)
1312

1413
useEffect(() => {
1514
if (loaded) {
1615
ExpoSplashScreen.hideAsync()
16+
setReady(true)
1717
}
1818
}, [loaded])
1919

20-
return null
20+
return ready
2121
}

0 commit comments

Comments
 (0)