-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: preloading for simple navigators - tabs, drawer (#11709)
**Motivation** This is a straightforward application of #11702. Now, we implement the behaviour of simple navigators. We refer to the original PR for a detailed motivation. Additionally, we have tests. **Test plan** The flow is sufficiently covered in the example app. https://github.com/react-navigation/react-navigation/assets/25709300/06622b0e-a0c0-4c73-8bf2-305495ddfc72
- Loading branch information
Showing
7 changed files
with
175 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import type { BottomTabScreenProps } from '@react-navigation/bottom-tabs'; | ||
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'; | ||
import { Button } from '@react-navigation/elements'; | ||
import * as React from 'react'; | ||
import { useEffect, useState } from 'react'; | ||
import { StyleSheet, Text, View } from 'react-native'; | ||
|
||
type PreloadBottomTabsParams = { | ||
Home: undefined; | ||
Details: undefined; | ||
}; | ||
|
||
const DetailsScreen = ({ | ||
navigation, | ||
}: BottomTabScreenProps<PreloadBottomTabsParams, 'Details'>) => { | ||
const [loadingCountdown, setLoadingCountdown] = useState(3); | ||
useEffect(() => { | ||
const interval = setInterval(() => { | ||
setLoadingCountdown((loadingCountdown) => { | ||
if (loadingCountdown === 1) { | ||
clearInterval(interval); | ||
} | ||
return loadingCountdown - 1; | ||
}); | ||
}, 1000); | ||
return () => clearInterval(interval); | ||
}, []); | ||
|
||
return ( | ||
<View style={styles.content}> | ||
<Text style={[styles.text, styles.countdown]}> | ||
{loadingCountdown > 0 && loadingCountdown} | ||
</Text> | ||
<Text style={styles.text}> | ||
{loadingCountdown === 0 ? 'Loaded!' : 'Loading...'} | ||
</Text> | ||
<Button onPress={navigation.goBack} style={styles.button}> | ||
Back to home | ||
</Button> | ||
</View> | ||
); | ||
}; | ||
|
||
const HomeScreen = ({ | ||
navigation, | ||
}: BottomTabScreenProps<PreloadBottomTabsParams, 'Home'>) => { | ||
const { navigate, preload, removePreload } = navigation; | ||
|
||
return ( | ||
<View style={styles.content}> | ||
<Button onPress={() => preload('Details')} style={styles.button}> | ||
Preload screen | ||
</Button> | ||
<Button onPress={() => navigate('Details')} style={styles.button}> | ||
Navigate | ||
</Button> | ||
<Button onPress={() => removePreload('Details')} style={styles.button}> | ||
Remove preload | ||
</Button> | ||
</View> | ||
); | ||
}; | ||
|
||
const BottomsTabs = createBottomTabNavigator<PreloadBottomTabsParams>(); | ||
|
||
export function TabPreloadFlow() { | ||
return ( | ||
<BottomsTabs.Navigator screenOptions={{ headerShown: false }}> | ||
<BottomsTabs.Screen name="Home" component={HomeScreen} /> | ||
<BottomsTabs.Screen name="Details" component={DetailsScreen} /> | ||
</BottomsTabs.Navigator> | ||
); | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
content: { | ||
flex: 1, | ||
padding: 16, | ||
justifyContent: 'center', | ||
}, | ||
button: { | ||
margin: 8, | ||
}, | ||
text: { | ||
textAlign: 'center', | ||
margin: 8, | ||
}, | ||
countdown: { | ||
fontSize: 24, | ||
minHeight: 32, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters