0

In the official docs of react-navigation it says:

It's highly recommended to use the methods on the navigation object instead of using action creators and dispatch. It should only be used for advanced use cases.

But why is that? I didn't find any explanation for this strong recommendation in the react-navigation docs.

With CommonActions (or NavigationActions in react-navigation smaller equal 4.x) I can nicely create navigation functions which I can access from anywhere without the need of passing a navigation prop around. And I didn't encounter issues with it which I could relate to it being a CommonAction instead of a part of the navigation prop - even in more complex scenarios with many screens and special routing arrangements.

1
  • I now also found the following reason in another part of the docs reactnavigation.org/docs/navigating-without-navigation-prop/…: Do not use this method when you have access to a navigation prop or useNavigation since it will behave differently, and many helper methods specific to screens won't be available. Commented Aug 6, 2020 at 12:10

1 Answer 1

1

There are several reasons:

  • Methods on the navigation object are strictly type-checked, i.e. if you use something like TypeScript or Flow, you'll get errors if you pass incorrect values, it's way more relaxed with action objects, which will only log error during runtime.
  • When a method exists, it means the related navigator is definitely rendered. You can't call openDrawer if you don't have a drawer, but you can call dispatch(DrawerActions.openDrawer()).
  • It's more typing navigate('Home') vs dispatch(CommonActions.navigate('Home'))

without the need of passing a navigation prop around

Not sure how action creators help with that, but you could just use useNavigation

Sign up to request clarification or add additional context in comments.

1 Comment

useNavigation fulfills a similar purpose as CommonActions in that it offers the use of a navigation prop without the need to manually pass it down to the component. Given this, useNavigation also has the disadvantage of the second reason you mentioned I guess.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.