简体   繁体   English

使用 React Navigation 5,如何使导航到抽屉项目重置该堆栈的 state?

[英]With React Navigation 5, how do I make navigating to a drawer item reset the state of that stack?

I am using React Navigation v5 and have a drawer navigator whose screens are a bunch of stack navigators.我正在使用 React Navigation v5 并有一个抽屉导航器,其屏幕是一堆堆栈导航器。

    <AppDrawer.Navigator drawerContent={({navigation, state}) => <DrawerScreen navigation={navigation} state={state}/>}>
      <AppDrawer.Screen name="TodayStack" component={TodayStackComponent}/>
      <AppDrawer.Screen name="ClientsStack" component={ClientsStackComponent}/>
      <AppDrawer.Screen name="SettingsStack" component={SettingsStackComponent}/>
    </AppDrawer.Navigator>

In DrawerScreen (which is my custom override of the Drawer component), I have it currently navigating to each drawer item like this:DrawerScreen (这是我对 Drawer 组件的自定义覆盖)中,我目前将它导航到每个抽屉项目,如下所示:

props.navigator.navigate('TodayStack')

That works fine in general.一般来说,这很好用。 However, I also want it to reset the stack state when you navigate from the drawer.但是,我还希望它在您从抽屉导航时重置堆栈 state。 For example, I may have 3 stacked screens in the navigation history of TodayStack .例如,我在TodayStack的导航历史中可能有 3 个堆叠的屏幕。 When I go to another drawer item, then tap back to TodayStack , I want it to reset back to the initial route for TodayStack 's navigator.当我 go 到另一个抽屉项目时,然后点击回到TodayStack ,我希望它重置回TodayStack导航器的初始路线。

Any tips on how to do this?关于如何做到这一点的任何提示?

try the following:尝试以下操作:

import { CommonActions, useNavigation } from '@react-navigation/native';从 '@react-navigation/native' 导入 { CommonActions, useNavigation };

then replace your code:然后替换您的代码:

props.navigator.navigate('TodayStack')

with

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Stack Actions在反应导航中重置路线? - How I can reset route in react-navigation with Stack Actions? 如何在反应导航中从抽屉堆栈导航到开关堆栈? - How can i navigate from a Drawer Stack to a Switch Stack in react navigation? React Navigation 5 - 在导航到它之前从不同选项卡中的另一个堆栈重置堆栈(类似于 popToTop()) - React Navigation 5 - Reset a stack (similar to popToTop()) from another stack in a different tab before navigating to it 使用 React Navigation,如何强制抽屉导航器弹出到堆栈顶部? - Using React Navigation, how can I force the drawer navigator to pop to the top of the stack? React Native 中的堆栈导航和抽屉导航 - Stack navigation and Drawer navigation in React Native 如何在 React Native 中重置我的整个堆栈? - How do I reset my entire stack in React Native? 如何在模糊时重置堆栈导航器? (反应原生,反应导航) - How to reset a Stack Navigator on blur? (React Native, React Navigation) 如何在堆栈导航中为 Webview 制作后退按钮 - How do I make a back button for Webview in the stack Navigation 如何清除/重置 React (JSX) 中的数组内部状态 - How do I clear/reset an array inside state in React (JSX) 如何在 React 中使用 onClick 重置 state 的特定部分 - How do I reset a specific part of a state with onClick in React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM