Home > Software design >  Is there any benefit of getting navigation prop using useNavigation hook in React Native?
Is there any benefit of getting navigation prop using useNavigation hook in React Native?

Time:03-18

Im now working on the project where they get navigation prop ONLY using useNavigation(), even if it is available in components prop and same with route, is there any benefit?

CodePudding user response:

No, there isn't any benefit to use the useNavigation hook as the default method for accessing the navigation prop and I personally would strongly advise against it. The official documentation is very clear on when to use the useNavigation hook.

useNavigation is a hook which gives access to navigation object. It's useful when you cannot pass the navigation prop into the component directly, or don't want to pass it in case of a deeply nested child.

If the component is defined as a Screen in the Navigator and the navigation prop is passed by it to the screen anyway, we should not create a dependency to a hook which we do not need.

We could even pass the navigation prop from the parent to its children if the children need it. Thus, there is no general need to use useNavigation at all and certainly not as the default method.

However, in some cases it is very inconvenient to pass the navigation prop very deep. That is why useNavigation exists.

  • Related