Skip to content
Discussion options

You must be logged in to vote

If I use context, I don't think I can preserve these values based on the navigation history

What do you mean by this?

The current recommended way is to use context:

const OptionsRouteContext = React.createContext<TypeOfParams | undefined>(undefined)

const rootStack = createNativeStackNavigator({
  screens: {
    Home: HomeScreen,
    OptionsTab: {
      screen: OptionsTab,
      layout: ({ route, children }) => {
        return (
          <OptionsRouteContext.Provider value={route.params}>
            {children}
          </OptionsRouteContext.Provider>
        );
      }
    }
  },
});

And then use the context in child components.

In React Navigation 8, you'll be able to do useRoute(…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@cetfu
Comment options

@satya164
Comment options

@cetfu
Comment options

Answer selected by cetfu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants