I don't know if I'm asking this question right but here goes. I use the Context API for storing global state. When the app loads, I'm displaying a Splash Screen (I do this natively and I'm not building a managed app / Expo). In the background I want to load some data into the global context object (in this example it's UserProfileContext). When this is complete I will display the main navigation. I think the code will make it clear what I'm trying to do.
The problem is that I don't have access to the global context until I display the navigation routes because I use the Context objects to wrap the navigation component. How can I accomplish what I'm trying to do?
If there is a better way to load some data before choosing my route I am willing to change the structure of the navigation and/or app.
Here is my code for the navigation:
const Stack = createStackNavigator()
const Drawer = createDrawerNavigator()
function CheckinStack() {
return (
<Stack.Navigator headerMode={'none'}>
<Stack.Screen
name={'Search Locations'}
component={SearchLocationsScreen}
/>
<Stack.Screen
name={'Check In Form'}
component={CheckInFormScreen}
/>
<Stack.Screen
name={'Checked In'}
component={CheckedInScreen}
/>
<Stack.Screen
name={'Business Details'}
component={BusinessDetailsScreen}
/>
</Stack.Navigator>
)
}
function MainDrawer() {
const {updateUserProfile} = useContext(UserProfileContext);
const [isLoading, setIsLoading] = useState(true)
const load = async () => {
try {
const profile = await retrieveUserProfile()
profile && updateUserProfile(profile)
setIsLoading(false)
} catch (e) {
}
}
if(isLoading){
return <LoadingScreen setIsLoading={setIsLoading}/>
}
return (
<Drawer.Navigator
drawerStyle={{
width: Dimensions.get('window').width > 600 ? '50%' : '70%',
maxWidth: 400,
}}
drawerContent={(props) => <CustomDrawerContent {...props} dataLoaded />}>
<Drawer.Screen name={'Search Locations'} component={CheckinStack} />
<Drawer.Screen name={'About'} component={AboutScreen} />
<Drawer.Screen name={'Favorites'} component={FavoritesScreen} />
<Drawer.Screen name={'Profile'} component={ProfileScreen} />
<Drawer.Screen name={'Report Issues'} component={ReportIssuesScreen} />
</Drawer.Navigator>
)
}
const NavContainer = () => {
return (
<NavigationContainer>
<UserLocationProvider>
<BusinessLocationsProvider>
<UserProfileProvider>
<CheckInProvider>
<FavoritesProvider>
<MainDrawer />
</FavoritesProvider>
</CheckInProvider>
</UserProfileProvider>
</BusinessLocationsProvider>
</UserLocationProvider>
</NavigationContainer>
)
}