React native 在react native中使用底部导航时,如何跟踪屏幕标题?
我在React Native中使用底部导航,我有一个标题,应该显示我现在在哪个屏幕上。我不知道如何更改选项卡栏上的文本。我试图利用onPress事件,但似乎不起作用 这是我的密码:React native 在react native中使用底部导航时,如何跟踪屏幕标题?,react-native,react-navigation-bottom-tab,React Native,React Navigation Bottom Tab,我在React Native中使用底部导航,我有一个标题,应该显示我现在在哪个屏幕上。我不知道如何更改选项卡栏上的文本。我试图利用onPress事件,但似乎不起作用 这是我的密码: <View style={styles.container}> <StatusBar barStyle="light-content" /> <View style={styles.header}>
<View style={styles.container}>
<StatusBar barStyle="light-content" />
<View style={styles.header}>
<Text style={styles.headerText}>{headerText}</Text>
<SettingsIcon />
</View>
<View style={styles.main}>
<NavigationContainer >
<Tab.Navigator initialRouteName="Home" tabBarOptions={{
activeTintColor: '#FF9F0A',
inactiveTintColor:'white',
style: {
backgroundColor: '#000000',//color you want to change
}
}}>
<Tab.Screen name="Home" component={Home} options={{
tabBarLabel: 'HOME',
tabBarIcon: ({ color, size }) => (
<HomeTabIcon name="home" color={color} size={size} />
),
}}/>
<Tab.Screen name="Controls" component={Controls} options={{
tabBarLabel: 'CONTROLS',
tabBarIcon: ({ color, size }) => (
<ControlsTabIcon name="controls" color={color} size={size} />
),
}}/>
<Tab.Screen name="Charging" component={Charging} options={{
tabBarLabel: 'CHARGING',
tabBarIcon: ({ color, size }) => (
<ChargingTabIcon name="charging" color={color} size={size} />
),
}}/>
</Tab.Navigator>
</NavigationContainer>
</View>
</View>
{headerText}
(
),
}}/>
(
),
}}/>
(
),
}}/>
您可以将选项卡导航器嵌套在屏幕导航器中
您可以根据活动屏幕的名称更改屏幕导航器标题的标题
欲了解更多信息,请阅读
从“React”导入React;
从'@react-navigation/native'导入{NavigationContainer,getFocusedRootNameFromRoute};
从'@react navigation/stack'导入{createStackNavigator};
const RootStack=createStackNavigator();
const Tab=createBottomTabNavigator();
功能getHeaderTitle(路线:任意){
//如果没有找到聚焦路线,我们需要假设它是初始屏幕
//如果屏幕内没有任何导航,则可能会发生这种情况
//在我们的例子中,它是“提要”,因为这是导航器中的第一个屏幕
const routeName=GetFocusedLouteNameFromRoute(route)?“Home”;
console.log(“name route=,routeName”);
交换机(路由名称){
“家”案例:
返回'name header tab1';
案例“表1”:
返回'name header tab1';
案例“表2”:
返回'name header tab2';
}
}
常量HomeTabs=()=>{
返回(
);
};
函数根(){
返回(
({
headerTitle:getHeaderTitle(路线),
})}
/>
);
};代码>