3

So, there is a screen that I'm using a javascript navbar instead of the react-native-navigation one (because of this bug).

I need to show a back button only if it can go back (if it's not the first screen in the stack).

Any way to check if it can go back? Something like navigator.canBoBack() or navigator.getCurrentScreenStack().lenght > 1.

1
  • @Siggy thanks but my issue is specific to the react-native-navigation library for react-native, not the browser. I've added it to the title to be more clear. Dec 20, 2017 at 23:11

2 Answers 2

6

Check if the screen's commandType prop is either Push or ShowModal and call set buttons accordingly.

const canGoBack =
      this.props.commandType === 'Push' ||
      this.props.commandType === 'ShowModal'
1
  • 1
    Thanks! That does solve the question. I edited to add a code snippet. Dec 22, 2017 at 13:09
0

You may track if your able to go back like this :

const prevGetStateForAction = Navigator.router.getStateForAction;

Navigator.router.getStateForAction = (action, state) => {
  // Do not allow to go back from Login
  if (action.type === "Navigation/BACK" && state && state.routes[state.index].routeName === "Login") {
    return null;
  }
  // Do not allow to go back to Login
  if (action.type === "Navigation/BACK" && state) {
    const newRoutes = state.routes.filter(r => r.routeName !== "Login");
    const newIndex = newRoutes.length - 1;
    return prevGetStateForAction(action, { index: newIndex, routes: newRoutes });
  }
  return prevGetStateForAction(action, state);
};

See this : https://github.com/react-community/react-navigation/issues/295

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.