版本:

expo v39.x
react-navigation 5.x
react native hook
Typescript

默认页面标题是调取 name字段的值

react-navigation 5.x createBottomTabNavigator 动态修改tabbar的页面标题

react-navigation 5.x createBottomTabNavigator 动态修改tabbar的页面标题

期望是标题分别显示对应tabbar的标题,如:首页、关注、热门

react-navigation 5.x createBottomTabNavigator 动态修改tabbar的页面标题

解决方案:

修改 index.tsx 文件

注意,这里不能在BottomTabNavigator.tsx上修改,会造成错误 Warning: Cannot update a component from inside the function body of a different component.

react-navigation 5.x createBottomTabNavigator 动态修改tabbar的页面标题

正确写法如下:

react-navigation 5.x createBottomTabNavigator 动态修改tabbar的页面标题

//...
function RootNavigator() {
  return (
    <Stack.Navigator>
      <Stack.Screen
        name="Root"
        component={BottomTabNavigator}
        options={({ route }: { route: any }) => {
          const routeName = route.state?.routes[route.state?.index]?.name;
          let title = '首页';
          if (routeName === 'Home') {
            title = '首页';
          } else if (routeName === 'Look') {
            title = '关注';
          } else if (routeName === 'Hot') {
            title = '热门';
          }
          return {
            title: title,
            headerStyle: {
              backgroundColor: Colors.primaryColor,
            },
            headerTintColor: '#fff',
          };
        }}
      />
      // ...
</Stack.Navigator>
)
}

通过 routeName 来手动设置每个页面的标题

相关文章:

  • 2022-02-24
  • 2021-11-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-18
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-05-20
  • 2022-12-23
  • 2021-06-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案