How to use useNavigation() within @react-navigation drawer?
This is possible using https://reactnavigation.org/docs/en/navigating-without-navigation-prop.html
Create RootNavigation
:
import { createRef } from 'react';
export const navigationRef = createRef();
export function navigate(name, params) {
navigationRef.current?.navigate(name, params);
}
export function goBack() {
navigationRef.current?.goBack();
}
In App.js
:
import { navigationRef } from './navigation/RootNavigation';
// ...
<NavigationContainer
ref={navigationRef}
>
{children}
</NavigationContainer>
Then import RootNavigation
from any source and you will not have to worry about the react tree.