从导航到另一个屏幕后将屏幕从堆栈中移除

huangapple go评论61阅读模式
英文:

Removing screen from Stack after navigating to another screen

问题

我有一个包含两个标签的 TabNavigator,分别是 "A 标签" 和 "B 标签"。
在 "B 标签" 中,有一个 StackNavigator,名为 "C 栈"。在这个堆栈内,我通过 navigation.navigate('A 标签') 导航到了 "A 标签"。我要如何重置 "B 标签",以便当用户再次导航到 "B 标签" 时,不再显示堆栈,而是显示 TabNavigator?

我尝试了以下方法:

navigation.reset({
  index: 0,
  routes: [{ name: 'B Tab' }],
});

这会移除堆栈,但会导航到 "B 标签" 屏幕,这不是我想要的。我希望留在当前屏幕。

英文:

I have a TabNavigator with two Tabs A Tab and B Tab.
In B, there is a StackNavigator C Stack. From within this stack, I navigate via navigation.navigate('A Tab') to Tab A. How can I reset B Tab, that when the user navigates back to B Tab, there is not the Stack, but the TabNavigator?

I tried

  navigation.reset({
    index: 0,
    routes: [{name: 'B Tab'}],
  });

This removes the tack, but this navigates to the sreen B Tab which I do not want. I want to stay on current screen.

答案1

得分: 1

尝试这样做

    import { CommonActions } from '@react-navigation/native';
    
    navigation.dispatch(
      CommonActions.reset({
        index: 0,
        routes: [
          { name: 'B Tab' }
        ],
      })
    );
英文:

try this

import { CommonActions } from '@react-navigation/native';

navigation.dispatch(
  CommonActions.reset({
    index: 0,
    routes: [
      { name: 'B Tab' }
    ],
  })
);

huangapple
  • 本文由 发表于 2023年2月19日 20:03:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500016.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定