尝试.resetRoot()
而不是.reset()
。我认为.reset()
需要状态作为参数。
我正在尝试从组件外部导航我的应用程序。具体来说,我正在使用访存拦截器,并且每当收到错误响应时,我都希望进行导航。
我在这里遵循了示例: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也是当前的before该应用程序尝试进行导航] >>
我正在尝试从组件外部导航我的应用程序。具体来说,我正在使用访存拦截器,并且每当接收到错误响应时,我都希望进行导航。我在这里遵循了示例:...
尝试.resetRoot()
而不是.reset()
。我认为.reset()
需要状态作为参数。
尝试.resetRoot()
而不是.reset()
。我认为.reset()
需要状态作为参数。