react-native - 如何在 React Native 中获取活动的 TabBarButton?
问题描述
我试图强调活动的文本,tabBarButton
但它似乎没有提供focused prop
. 我怎样才能做到这一点 ?
<Tab.Screen
name="Home"
component={HomeNavigator}
options={() => ({
title: 'Home',
tabBarButton: (props) => (
<TouchableWithoutFeedback {...props} >
<View style={{flex:2, alignItems: 'center'}}>
<Image source={LOGO} resizeMode="contain" style={[styles.imgSize, props.focused && { opacity:1 }]} />
<Text style={[styles.label, props.focused && { opacity:1, textDecorationLine: 'underline' }]}>HOME</Text>
</View>
</TouchableWithoutFeedback>
),
})}
/>
解决方案
你应该把你包裹Tab.Screen
在 aTab.Navigator
中,因为focused
道具在navigator
(这里)
<Tab.Navigator
initialRouteName={"Home"}
screenOptions={({ route }) => ({
tabBarIcon: ({ focused, color, size }) => {
let imageStyle
let textStyle
if (route.name === "Home") {
imageStyle = focused ? styles.activeImageStyle : styles.inactiveImageStyle
textStyle = focused ? styles.activeTextStyle : styles.inactiveTextStyle
}
if (route.name === "OtherScreen") {
imageStyle = focused ? styles.activeImageStyle : styles.inactiveImageStyle
textStyle = focused ? styles.activeTextStyle : styles.inactiveTextStyle
}
return <TabBarIcon imageStyle={imageStyle} textStyle={textStyle}/>
},
headerStatusBarHeight: 0,
}
)}
tabBarOptions={{
showLabel: false,
style: styles.tabBarStyle,
keyboardHidesTabBar: true,
safeAreaInsets: { bottom: 0 }
}}
>
<Tab.Screen
name="Home"
component={HomeNavigator}
/>
<Tab.Screen
name="OtherScreen"
component={OtherNavigator}
/>
</Tab.Navigator>
// And your styles could be
const styles = export default StyleSheet.create({
activeImageStyle: {...},
inactiveImageStyle: {...},
activeTextStyle: {...},
inactiveTextStyle: {...}
})
// Where you can declare you TabBarIcon in another file as being this
<TouchableWithoutFeedback {...props} >
<View style={{flex:2, alignItems: 'center'}}>
<Image source={LOGO} resizeMode="contain" style={[styles.imgSize, props.imageStyle ]} />
<Text style={[styles.label, props.textStyle]}>HOME</Text>
</View>
</TouchableWithoutFeedback>
推荐阅读
- javascript - 为什么 React 路由在组件中不起作用?
- reactjs - 承诺解决时反应重定向?
- python - 包数据不包含在包中
- c# - NRecoPdfRenderer - 内存不足异常
- python - URL 数据不会进入 SQL 数据库 Python
- java - 在 Android 应用程序中存储 MongoDB 数据库凭据
- javascript - Rxjs iif 或简单的 if
- python - 您如何就地重新投影 geopandas GeoSeries?
- c# - 您可以在使用 Roslyn 生成的编译中包含 Fody 吗?
- javascript - 如何更改在 TinyMCE 中插入图像的功能?