typescript - 如何在 TypeScript 中使用 defaultNavigationOptions 的属性?
问题描述
当我们尝试在 Typescript 的 defaultNavigationOptions 中使用 tabBarLabel 属性时,我们会收到关于类型的错误。
我已经尝试了 Javascript 中的代码,但没有出现错误。如果我们只使用 barTabIcon 代码可以工作。
我已经安装了以下类型的库:'@types/react-navigation[...]' 但什么也没有。
我怎么解决这个问题?有任何想法吗?
import React, {Component} from 'react';
import {StyleSheet, Image, View} from 'react-native';
import {
createAppContainer,
createBottomTabNavigator
} from 'react-navigation';
import AccountScreen from '../tabmenu/AccountScreen';
import CarteScreen from '../tabmenu/CarteScreen';
import OperazioniScreen from '../tabmenu/OperazioniScreen';
const TabNavigator = createBottomTabNavigator({
Account: {screen: AccountScreen},
Carte: {screen: CarteScreen},
Operazioni: {screen: OperazioniScreen}
}, {
defaultNavigationOptions: ({navigation}) => ({
tabBarIcon: ({focused, horizontal, tintColor}) => {
//inserire switch
const {routeName} = navigation.state;
if (routeName === 'Account') {
return (
<Image
source= .
{require('../../../res/drawable/faq.png')}
style={{width: 20, height: 20,}}/>
);
} else {
return (
<Image
source= .
{require('../../../res/drawable/faq.png')}
style={{width: 20, height: 20}}/>
)
}
},
tabBarLabel: ({focused, tintColor}) => {
const {routeName} = navigation.state;
let label;
switch (routeName) {
case 'Account':
return label = focused ?<Text>Account</Text>:null;
case 'Carte':
return label = focused ? <Text >Carte</Text> :null;
case 'Appointments':
return label = focused ?<Text>Operazioni</Text> : null;
}
return label
},
}
),
}
);
class HomeScreen extends Component {
render() {
return (
<View style={styles.container}>
<TabNavigator/>
</View>
);
}
}
const styles = StyleSheet.create({
[...]
});
export default createAppContainer(TabNavigator);
解决方案
反应导航的类型非常复杂。并且可用性与 React 的类型不匹配。我花了太多时间来弄清楚类型,我希望没有人再这样做了。所以给你。
- 的第二个参数
createBottomTabNavigator()
是类型
BottomTabNavigatorConfig
。 - 它的属性
defaultNavigationOptions
是类型NavigationBottomTabScreenOptions
或
(
navigationOptionsContainer: NavigationScreenConfigProps & {
navigationOptions: NavigationScreenConfig<NavigationBottomTabScreenOptions>;
}
) => NavigationBottomTabScreenOptions
此函数需要返回
NavigationBottomTabScreenOptions
您感兴趣的两个属性:tabBarIcon
类型的属性React.ReactElement<any>
或
(options: TabBarIconProps) => React.ReactElement<any> | null
tabBarLabel
类型的属性string
orReact.ReactElement<any>
或
(options: TabBarLabelProps) => React.ReactElement<any> | string | null
这使您的函数tabBarIcon
看起来像这样:
const tabBarIcon = (options: TabBarIconProps):React.ReactElement<any> => {
const {focused, horizontal, tintColor} = options;
const {routeName} = navigation.state;
if (routeName === 'Account') {
return <Image {...}/>;
} else {
return <Image {...}/>;
}
}
和这样的功能tabBarLabel
:
const tabBarLabel = (options:TabBarLabelProps):React.ReactElement<any>|null => {
const {focused, tintColor} = options;
const {routeName} = navigation.state;
// I've removed the undefined variable "label" and added instead a default clause
switch (routeName) {
case 'Account':
return label = focused ? (<Text>Account</Text>) : null;
case 'Carte':
return label = focused ? <Text >Carte</Text> : null;
case 'Appointments':
return label = focused ? <Text>Operazioni</Text> : null;
default:
return null;
}
}
所以一旦这一切都结束了,我们就可以回到defaultNavigationOptions
const defaultNavigationOptions = (navigationOptionsContainer: NavigationScreenConfigProps):NavigationBottomTabScreenOptions => {
return {
tabBarIcon: tabBarIcon,
tabBarLabel: tabBarLabel,
}
}
然后添加缺少的导入,你应该完成。
推荐阅读
- c++ - 使用 yocto sdk 交叉编译 pistache
- asp.net-core - 如何更改身份服务中的默认令牌提供程序
- sql - 在 2 个字段条件下使用 case
- c - Why isn't the unsigned long long int showing the right value in C?
- spark-ar-studio - Spark AR 的图像模糊问题
- reactjs - 如何有条件地验证登录和渲染(ReactJS 和 Firebase)?
- django - 来自外部数据库的 Django ForeignKey
- mongodb - 如何从 mongo 聚合中获得想要的结果
- ruby-on-rails - 在 Travis-CI 上测试 Actionmailer
- python - 迭代 Mock 返回值