How to reset a specific stack navigator from outside of it?

我怕爱的太早我们不能终老 提交于 2019-12-02 10:07:25

Normally, if you were in a specific stack, you would reset via this.props.navigation.reset() but since you want to reset one stack from another stack you will need to use a NavigationService for the StackNavigator that needs to be reset.

Read this help guide to see how you can create a NavigationService.

Once you have a NavigationService setup, edit the NavigationService.js to this:

import { StackActions, NavigationActions } from 'react-navigation';

let _navigator;

function setTopLevelNavigator(navigatorRef) {
  _navigator = navigatorRef;
}

function reset(index = 0, actions) {
  _navigator.dispatch(
    StackActions.reset({
      index,
      actions
    })
  );
}

// add other navigation functions that you need and export them

export default {
  reset,
  setTopLevelNavigator,
};

Then from any function in any file, do this:

import {reset} from NavigationService.js
//your other code and functions here.
reset(0, actions)

Read more about it here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!