Are goBack() and pop() without arguments functionally identical?

Viewed 1603

From reading the documentation it would seem so, but I suppose I'm confused as to why it isn't called out that they're interchangeable when pop() is called without arguments, or why there would even be two different functions with such similar behavior (and why goBack() wouldn't also take an argument for the number of screens to go back)?

pop - go back in the stack

2 Answers

The difference is:

  • pop is specific to stack navigator, accepts arguments like the number of screens to pop which is relevant for the stack navigator
  • goBack is more general, it works in any navigator: stack, tabs drawer

It's not exactly interchangable since it depends on which navigator are you in. For example, if your screen is in a tab navigator nested in stack navigator, if you use pop(), it'll go back in the parent stack navigator, but if you call goBack(), it'll go back in the tab navigator (depending on if there are any screens to go back in both cases).

So generally you probably want to use goBack() which will do the appropriate behavior in most cases, and use pop() only if you have specific requirements and want the specific behaviour it provides.

With the help of pop you can go back a few screens back in a stack

goBack takes you back to the last screen

see here

The pop action takes you back to a previous screen in the stack. It takes one optional argument (count), which allows you to specify how many screens to pop back by.

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

StackActions.pop(2);

Related