-
-
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 stack navigator (#11733)
This is yet another follow-up of #11702. Now, it is about implementing preloading for the stack navigator. The first essential part is merged from #11727 and is about creating temporary descriptors for routes which are not yet in the navigation state, but only preloaded. The remaining part is adjusting the logic for the preloading flow. Particularly, a lot of changes were needed for handling cards, where `gesture` is `undefined` and making the animation run a later for those routes (not on the initial mount, but on navigating). The remaining changes are not critical. Precise comments are included in the comments. Please review the tests and the video below. https://github.com/react-navigation/react-navigation/assets/25709300/830a7dfc-cf6a-4117-b38d-7f6326934af1 --------- Co-authored-by: Satyajit Sahoo <satyajit.happy@gmail.com>
- Loading branch information
Showing
10 changed files
with
473 additions
and
195 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,117 @@ | ||
import { Button } from '@react-navigation/elements'; | ||
import { | ||
createStackNavigator, | ||
type StackScreenProps, | ||
} from '@react-navigation/stack'; | ||
import * as React from 'react'; | ||
import { useEffect, useState } from 'react'; | ||
import { StyleSheet, Text, View } from 'react-native'; | ||
|
||
type PreloadStackParams = { | ||
Home: undefined; | ||
Details: undefined; | ||
Profile: undefined; | ||
}; | ||
|
||
const DetailsScreen = ({ | ||
navigation, | ||
}: StackScreenProps<PreloadStackParams, 'Details'>) => { | ||
const [loadingCountdown, setLoadingCountdown] = useState(3); | ||
useEffect(() => { | ||
const interval = setInterval(() => { | ||
setLoadingCountdown((loadingCountdown) => { | ||
if (loadingCountdown === 1) { | ||
clearInterval(interval); | ||
} | ||
return loadingCountdown - 1; | ||
}); | ||
}, 1000); | ||
}, []); | ||
|
||
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> | ||
<Button | ||
onPress={() => navigation.navigate('Profile')} | ||
style={styles.button} | ||
> | ||
Go to Profile | ||
</Button> | ||
</View> | ||
); | ||
}; | ||
|
||
const ProfileScreen = ({ | ||
navigation, | ||
}: StackScreenProps<PreloadStackParams, 'Profile'>) => { | ||
return ( | ||
<View style={styles.content}> | ||
<Text style={styles.text}>Profile</Text> | ||
<Button onPress={navigation.goBack} style={styles.button}> | ||
Back to home | ||
</Button> | ||
</View> | ||
); | ||
}; | ||
|
||
const HomeScreen = ({ | ||
navigation, | ||
}: StackScreenProps<PreloadStackParams, 'Home'>) => { | ||
const { navigate, preload, removePreload } = navigation; | ||
|
||
return ( | ||
<View style={styles.content}> | ||
<Button onPress={() => preload('Details')} style={styles.button}> | ||
Preload Details | ||
</Button> | ||
<Button onPress={() => preload('Profile')} style={styles.button}> | ||
Preload Profile | ||
</Button> | ||
<Button onPress={() => navigate('Details')} style={styles.button}> | ||
Navigate Details | ||
</Button> | ||
<Button onPress={() => removePreload('Details')} style={styles.button}> | ||
Remove Details preload | ||
</Button> | ||
</View> | ||
); | ||
}; | ||
|
||
const SimpleStack = createStackNavigator<PreloadStackParams>(); | ||
|
||
export function StackPreloadFlow() { | ||
return ( | ||
<SimpleStack.Navigator screenOptions={{ headerShown: false }}> | ||
<SimpleStack.Screen name="Home" component={HomeScreen} /> | ||
<SimpleStack.Screen name="Details" component={DetailsScreen} /> | ||
<SimpleStack.Screen name="Profile" component={ProfileScreen} /> | ||
</SimpleStack.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
Oops, something went wrong.