1

在反应导航中,如何替换堆栈中的最后两个屏幕?

例如,如果我当前的堆栈是screen1 -> screen2 -> screen3,当我.replace('screen4')在 screen3 上调用时,它变为screen1 -> screen2 -> screen4

但我想要一种方法让它变成screen1 -> screen4

4

1 回答 1

2

你需要使用reset来做到这一点:

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

// ...

navigation.dispatch(state => {
  // Remove the last 2 routes from current list of routes
  const routes = state.routes.slice(0, -2);

  // Reset the state to the new state with updated list of routes
  return CommonActions.reset({
    ...state,
    index: routes.length - 1,
    routes
  });
});
于 2020-02-21T04:12:00.143 回答