Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

How to set the initial navigation theme by route params from outside of the StackNavigator

$
0
0

I am trying to set the initial navigation theme based on the current route params. But I have to render the NavigationContainer to access the ref to gain information aboute the route, which leads to a flash of content with the default theme. Can I access the route without the ref from outside of the NavigationContainer somehow?

Here is what i tried, which leads to a flash of unthemed content:

export default function AuthNavigator() {  const { theme: { navigation: navigationTheme }, setThemeByTenantId } = useContext(ThemeContext)  // auth stuff // set custom theme on initial load  useEffect(()=> {    if (navigationRef.current?.getState()) {      const route = navigationRef.current?.getState().routes[0]      if (route && route.params && route.params.tenantId) {         setThemeByTenantId(route.params.tenantId)      }    }  },[navigationRef.current])  return (<View style={styles.container}><NavigationContainer        ref={navigationRef}        initialState={initialState}        onStateChange={handleStateChange}        theme={navigationTheme}        linking={linking}>        { signedIn ?<UserProvider><SignedInStack /></UserProvider> :<SignedOutStack />        }</NavigationContainer></View>  )}

Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>