`index` option in navigation.reset does nothing

10
closed
Maker-Mark
Maker-Mark
Posted 1 month ago

`index` option in navigation.reset does nothing #10056

Current behavior

Calling navigation.reset with index 0 or 1 results in the same behavior. The stack always just focuses on the "top" screen, the one at the last index, here index 1 (when zero indexing for this array). I cant find any documentation on what the index actually does, except for in CommonActions (https://reactnavigation.org/docs/navigation-actions/#reset) where the index sets the focusing screen in the stack, but that's not what I'm seeing here.

  navigation.reset({
            index: 0,
            routes: [
               {
                name: 'Profile',
              },
              {
                name: 'Settings',
              }, 
            ],
          })

Expected behavior

The index param set's what screen is focused. Ie index: 1 would

Reproduction

https://snack.expo.dev/@markclair/navigation-reset-does-not-effect-focused-screen

Platform

  • Android
  • iOS
  • Web
  • Windows
  • MacOS

Packages

Environment

package version
@react-navigation/native 6.0.6
@react-navigation/stack 6.0.11
@react-navigation/native-stack 6.0.0
@react-navigation/bottom-tabs NA
@react-navigation/drawer NA
@react-navigation/material-bottom-tabs NA
@react-navigation/material-top-tabs NA
npm or yarn yarn
github-actions[bot]
github-actions[bot]
Created 1 month ago

Couldn't find version numbers for the following packages in the issue:

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

Can you update the issue to include version numbers for those packages? The version numbers must match the format 1.2.3.

github-actions[bot]
github-actions[bot]
Created 1 month ago

Couldn't find version numbers for the following packages in the issue:

  • @react-navigation/bottom-tabs
  • @react-navigation/drawer
  • @react-navigation/material-bottom-tabs
  • @react-navigation/material-top-tabs

Can you update the issue to include version numbers for those packages? The version numbers must match the format 1.2.3.

The versions mentioned in the issue for the following packages differ from the latest versions on npm:

  • @react-navigation/native (found: 6.0.0, latest: 6.0.6)
  • @react-navigation/stack (found: 6.0.0, latest: 6.0.11)

Can you verify that the issue still exists after upgrading to the latest versions of these packages?

github-actions[bot]
github-actions[bot]
Created 1 month ago

Couldn't find version numbers for the following packages in the issue:

  • @react-navigation/bottom-tabs
  • @react-navigation/drawer
  • @react-navigation/material-bottom-tabs
  • @react-navigation/material-top-tabs

Can you update the issue to include version numbers for those packages? The version numbers must match the format 1.2.3.

The versions mentioned in the issue for the following packages differ from the latest versions on npm:

  • @react-navigation/stack (found: 6.0.0, latest: 6.0.11)

Can you verify that the issue still exists after upgrading to the latest versions of these packages?

github-actions[bot]
github-actions[bot]
Created 1 month ago

Couldn't find version numbers for the following packages in the issue:

  • @react-navigation/bottom-tabs
  • @react-navigation/drawer
  • @react-navigation/material-bottom-tabs
  • @react-navigation/material-top-tabs

Can you update the issue to include version numbers for those packages? The version numbers must match the format 1.2.3.

satya164
satya164
Created 1 month ago

index: 0 with 2 routes isn't a valid state object in stack navigator since index is always the last route in a stack. react navigation corrects invalid state and in this case it ignores index.

Maker-Mark
Maker-Mark
Created 1 month ago

@satya164 Hmm, so what would be a valid index? I'm having trouble connecting the stack order and index.

Thanks for the quick response! I saw your previous comment on another issue about the index, but it didn't clear this up.

satya164
satya164
Created 1 month ago

Valid index is 1 because the index of the last route is 1, however there's no need to pass an index here

Maker-Mark
Maker-Mark
Created 1 month ago

Valid index is 1 because the index of the last route is 1, however there's no need to pass an index here

@satya164 Okay, so I'm confused when it would be necessary to pass an index. If 1 is not needed since Settings is the last route in the stack anyway, why wouldn't 0 be valid, since 0 is the second screen in the stack, and it's Profile?

It just seems not to make sense that 0 and 1 would have no effect. Can you link me a snack that shows how you can use index and it actually having an effect on the reset?

Thanks

satya164
satya164
Created 1 month ago

Okay, so I'm confused when it would be necessary to pass an index

If you have a tab or drawer navigator. Or if you are passing a full state and not partial state (https://reactnavigation.org/docs/navigation-state/) like here.

github-actions[bot]
github-actions[bot]
Created 1 month ago

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.

Previous