以前的开发人员出于任何原因传递null,这给出了:
TypeError:null不是计算tabNavigation.navigate的对象。
代码如下:
import { NavigationRef } from './RootNavigation';
const renderBackToDashboard = (tabNavigation: any) => {
return <HeaderBackButton tintColor={defaultOptions.headerTintColor||'white'} labelVisible={defaultOptions.headerBackTitleVisible||false} onPress={() => { tabNavigation.navigate(SCREEN_NAMES.DASHBOARD_TAB); }} />
}
const testViews = (tabNavigation: any, isLoggedIn: boolean) => {
return (
<>
<Stack.Screen name={SCREEN_NAMES.LEGAL} component={LegalDisclosure} options={{ title: t('legal'), headerLeft: () => renderBackToDashboard(tabNavigation), headerRight: () => isLoggedIn ? renderLogo(tabNavigation) : null }} />
<Stack.Screen name={SCREEN_NAMES.DATA} component={DataProtection} options={{ title: t('data'), headerLeft: () => renderBackToDashboard(tabNavigation), headerRight: () => isLoggedIn ? renderLogo(tabNavigation) : null }} />
<Stack.Screen name={SCREEN_NAMES.GUEST} component={GUEST} options={{ title: t('guest'), headerLeft: () => renderBackToDashboard(tabNavigation), headerRight: () => isLoggedIn ? renderLogo(tabNavigation) : null }} />
</>
);
};
const getLoggedOutScreens = () => {
return (
<Stack.Navigator screenOptions={defaultOptions}>
<Stack.Screen name={SCREEN_NAMES.AUTH} component={AuthScreen} options={{ headerShown: false }} />
{testViews(null, false)}
<Stack.Screen name={SCREEN_NAMES.MY_SCREEN} component={MyScreen} options={{ title: t('myScreen') }} />
</Stack.Navigator>
);
};
return (
<NavigationContainer ref={NavigationRef} theme={MyTheme}>
{loggedIn ? getLoggedInScreens() : getLoggedOutScreens()}
{loggedIn ? <UserErrorComponent /> : null}
</NavigationContainer>
);
RootNavigation.ts
import React from 'react';
export const NavigationRef = React.createRef<any>();
class Nav {
navigate = (name: string, params?: any) => {
NavigationRef.current?.navigate(name, params);
};
}
export const RootNavigation = new Nav();
我试过了:{testViews(NavigationRef, false)} , {testViews(NavigationRef.current, false)} , {testViews({NavigationRef}, false)}
不起作用。我很高兴有任何建议。
在tabNavigation.navigate(SCREEN_NAMES.DASHBOARD_TAB);
处触发错误
2条答案
按热度按时间igsr9ssn1#
您的项目导航结构看起来很好。但是RootNavigation.ts可能需要像这样修改。
RootNavigation.ts
在此导航容器中包含的任何屏幕中,您可以使用以下代码进行导航。
你可以在react-navigation文档中看到更多https://reactnavigation.org/docs/navigating-without-navigation-prop/#usage
eimct9ow2#
下面的更改修复了我的错误: