我正在尝试从组件外部导航我的应用程序。具体来说,我正在使用抓取拦截器,并且希望在收到错误响应时进行导航。
我在这里遵循以下示例:https://reactnavigation.org/docs/navigating-without-navigation-prop/
但是,我的应用仍然给我一个错误,提示未渲染导航器或导航器尚未完成安装:
Screenshot of app with error message
据我所知,这两种情况都不适用。在我尝试实际导航之前,已使用导航器加载并渲染了该应用程序
我的App.jsx:
// ... imports and so on ...
fetchIntercept.register({
response: (response) => {
if (response.status === 401) {
// Unverified subscription
RootNavigation.reset({ index: 0, routes: [{ name: 'Intercept' }] });
}
return response;
},
});
{ ... }
const InterceptNavigator = createStackNavigator(
{
Application: {
screen: ApplicationScreen,
},
Intercept: {
screen: SubscriptionInterceptScreen,
},
},
{
initialRouteKey: 'Application',
},
);
const App = createAppContainer(InterceptNavigator);
export default () => {
React.useEffect(() => {
RootNavigation.isMountedRef.current = true;
return () => { RootNavigation.isMountedRef.current = false; };
}, []);
return (
<NavigationContainer ref={RootNavigation.navigationRef}>
<App />
</NavigationContainer>
);
};
RootNavigation.js:
import * as React from 'react';
export const isMountedRef = React.createRef();
export const navigationRef = React.createRef();
export function navigate(name, params) {
if (isMountedRef.current && navigationRef.current) {
navigationRef.current.navigate(name, params);
}
}
export function reset(options) {
if (isMountedRef.current && navigationRef.current) {
navigationRef.current.reset(options);
}
}
我还在整个过程中插入了许多控制台日志,所有这些日志都表明该应用程序已加载,该NavigationRef是当前的,并且isMountedRef也是当前的之前该应用程序尝试进行导航< / p>
答案 0 :(得分:0)
尝试使用.resetRoot()
而不是.reset()
。我认为.reset()
需要状态作为参数。
答案 1 :(得分:0)
找到了解决方案。问题是我混用了版本4和版本5的代码(并指的是混合文档)。
为解决此问题,我删除了对版本5代码的引用,然后按照此页面上的步骤操作以使导航器正常工作:https://reactnavigation.org/docs/4.x/navigating-without-navigation-prop/