官方文档:https://reactnavigation.org/docs/navigating-without-navigation-prop/
- 修改APP.js
import { NavigationContainer } from '@react-navigation/native';
import { navigationRef } from './RootNavigation';
export default function App() {
return (
<NavigationContainer ref={navigationRef}>{/* ... */}</NavigationContainer>
);
}
- 定义一个
RootNavigation.js
文件
// RootNavigation.js
import { createNavigationContainerRef } from '@react-navigation/native';
export const navigationRef = createNavigationContainerRef()
export function navigate(name, params) {
if (navigationRef.isReady()) {
navigationRef.navigate(name, params);
}
}
// add other navigation functions that you need and export them
- 在其他地方使用
// any js module
import * as RootNavigation from './path/to/RootNavigation.js';
// ...
RootNavigation.navigate('ChatScreen', { userName: 'Lucy' });
标签:拦截器,js,RootNavigation,navigationRef,export,跳转,import,页面
From: https://www.cnblogs.com/ikuns/p/17803054.html