Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When navigating to a stack, route state of this stack is undefined #11985

Open
5 of 12 tasks
matpoz opened this issue May 10, 2024 · 4 comments
Open
5 of 12 tasks

When navigating to a stack, route state of this stack is undefined #11985

matpoz opened this issue May 10, 2024 · 4 comments

Comments

@matpoz
Copy link

matpoz commented May 10, 2024

Current behavior

  1. I have a root stack and a nested stack.
const RootNavigator = () => (
  <RootStack.Navigator>
    <RootStack.Screen component={RootScreen} name="Screen" />
    <RootStack.Screen component={NestedNavigator} name="NestedStack" />
  </RootStack.Navigator>
);
  1. I navigate from from a screen within the root stack ("RootScreen") to a screen within the nested stack ("NestedStack").
  2. Check the route.state of this stack. Use any method: getState, useNavigationState, the route prop all give the same result.
const NestedNavigator = ({ route }) => {
  const currentRoute = useNavigationState((state) =>
    state.routes.find((route) => route.name === 'NestedStack')
  );

  return (
    <>
      // undefined
      <Text>Route state index: {route?.state?.index}</Text>
      // also undefined
      <Text>Route state index: {currentRoute?.state?.index}</Text>
      <NestedStack.Navigator>
        <NestedStack.Screen component={NestedScreen1} name="NestedScreen1" />
        <NestedStack.Screen component={NestedScreen2} name="NestedScreen2" />
      </NestedStack.Navigator>
    </>
  );
};
  1. To get access to the state property I have to perform any navigation action within the nested stack.

Expected behavior

The nested stack is rendered and so is a screen within the stack.
The state property containing the name and index of the currently rendered route should already be accessible.

Reproduction

https://snack.expo.dev/WDITwptdY68Ahe_pf0wwW

Platform

  • Android
  • iOS
  • Web
  • Windows
  • MacOS

Packages

  • @react-navigation/bottom-tabs
  • @react-navigation/drawer
  • @react-navigation/material-top-tabs
  • @react-navigation/stack
  • @react-navigation/native-stack
  • react-native-tab-view

Environment

  • I've removed the packages that I don't use
package version
@react-navigation/native 6.1.17
@react-navigation/stack 6.3.29
react-native-safe-area-context 4.5.0
react-native-screens 3.20.0
react-native-gesture-handler 2.9.0
react-native 0.72.6
@SusulAdam
Copy link

I have the same issue on react native 0.72.2 and similar navigation packages version.

@matpoz matpoz closed this as completed May 13, 2024
@AptypTheKing
Copy link

@matpoz why did you close it? you found another solution?

Copy link

Hey! This issue is closed and isn't watched by the core team. You are welcome to discuss the issue with others in this thread, but if you think this issue is still valid and needs to be tracked, please open a new issue with a repro.

@matpoz matpoz reopened this May 13, 2024
@matpoz
Copy link
Author

matpoz commented May 13, 2024

@AptypTheKing
I found this in the v5 to v6 migration guide:
https://reactnavigation.org/docs/upgrading-from-5.x/#no-more-state-property-on-the-route-prop

"The route prop passed to components often contained a state property which held state of the child navigator. While it wasn't meant to be public and we recommended against using it in the docs, we've seen a lot of people use this property.
It's problematic to use the property since it's not guaranteed to exist before navigation happens in the child navigator. This can cause subtle bugs in your app which you might not notice during development. So we have started warning on using this property in React Navigation 5 and removed this property entirely in React Navigation 6 prevent its usage."

Although this is not exactly the same case (I don't intend to use the route prop at all), this shows that this probably doesn't work by design.

I haven't found a solution.
I'd really appreciate any advice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants