最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React Native Expo Router Tabs: How to set Tab Name and Icon? - Stack Overflow

programmeradmin2浏览0评论

I am using expo-router in my React Native expo app, and my files are

├── app
│   ├── (main)
│   └── _layout.js
│   └── _foo.js
│   └── _bar.js

layout.js

import { Tabs } from "expo-router";
import { SafeAreaProvider } from "react-native-safe-area-context";

export default function Layout() {
    return (
      <SafeAreaProvider>
        <Tabs>
          <Tabs.Screen name="foo" screenOptions={{ title: "Foo" }} />
          <Tabs.Screen name="bar" screenOptions={{ title: "Bar" }} />
        </Tabs>
      </SafeAreaProvider>
    );
  }

However, the 2 tab buttons showing on my iOS device have titles foo and bar instead of Foo and Bar.

What is the correct way to set the names for the tab buttons?

Also, is there a way to rearrange the order of the tab buttons? Such as showing [ Foo | Bar ] instead of [ Bar | Foo ]. Thanks!

I am using expo-router in my React Native expo app, and my files are

├── app
│   ├── (main)
│   └── _layout.js
│   └── _foo.js
│   └── _bar.js

layout.js

import { Tabs } from "expo-router";
import { SafeAreaProvider } from "react-native-safe-area-context";

export default function Layout() {
    return (
      <SafeAreaProvider>
        <Tabs>
          <Tabs.Screen name="foo" screenOptions={{ title: "Foo" }} />
          <Tabs.Screen name="bar" screenOptions={{ title: "Bar" }} />
        </Tabs>
      </SafeAreaProvider>
    );
  }

However, the 2 tab buttons showing on my iOS device have titles foo and bar instead of Foo and Bar.

What is the correct way to set the names for the tab buttons?

Also, is there a way to rearrange the order of the tab buttons? Such as showing [ Foo | Bar ] instead of [ Bar | Foo ]. Thanks!

Share Improve this question edited Mar 24, 2023 at 6:08 gameveloster asked Mar 24, 2023 at 6:02 gamevelostergameveloster 1,5535 gold badges19 silver badges39 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 6

Just change screenOptions to options instead. And title to tabBarLabel

import { Tabs } from "expo-router";
import { SafeAreaProvider } from "react-native-safe-area-context";

export default function Layout() {
    return (
      <SafeAreaProvider>
        <Tabs>
          <Tabs.Screen name="foo" options={{ tabBarLabel: "Foo" }} />
          <Tabs.Screen name="bar" options={{ tabBarLabel: "Bar" }} />
        </Tabs>
      </SafeAreaProvider>
    );
  }

For icons, you can use the react-native-vector-icons library (Link for the library). Or you can follow this video.

For Title: Whatever name you will give in name prop of ponent will be displayed in tab bar. I have used these docs for the tab navigation.

import {createBottomTabNavigator} from '@react-navigation/bottom-tabs';
import 'react-native-gesture-handler';
import Icon from 'react-native-vector-icons/Ionicons';

export default function Layout() {
  const Tab = createBottomTabNavigator();
  return (
    <Tab.Navigator
      screenOptions={({route}) => ({
        tabBarIcon: ({focused, size}) => {
          let iconName;
          if (route.name === 'Foo') {
            iconName = focused ? 'home' : 'home-outline';
          } else if (route.name === 'Bar') {
            iconName = focused ? 'basketball' : 'basketball-outline';
          }
          return <Icon name={iconName} size={size} color={'pink'} />;
        },
        tabBarLabelStyle: {
          color: 'pink',
        },
        tabBarStyle: {
          backgroundColor: 'black',
        },
      })}>
      <Tab.Screen name="Foo" ponent={'You Component Name'} />
      <Tab.Screen name="Bar" ponent={'You Component Name'} />
    </Tab.Navigator>
  );
}

Setting the names-> use the options prop

<Tabs.Screen
  name="foo"
  options={{
    title: 'Foo'
  }}
/>

Rearrange your tabs -> simple define your tabs in the order you want them to appear

<Tabs>
  <Tabs.Screen name="foo" options={{ title: "Foo" }} />
  <Tabs.Screen name="bar" options={{ title: "Bar" }} />
</Tabs>

NB name refers to the name of your file so the file should be called foo.js

SafeAreaProvider is not needed.

Tabs.Screen options tabBarLabel and tabBarIcon. tabBarIcon has the following definition:

tabBarIcon?: ({
  focused: boolean;
  color: string;
  size: number;
}) => React.ReactNode) | undefined

Using FontAwesome as an example here, but other icon options will work here as well.

import { FontAwesome } from "@expo/vector-icons";
import { Tabs } from "expo-router";
import { SafeAreaProvider } from "react-native-safe-area-context";

export default function Layout() {
  return (
    <SafeAreaProvider>
      <Tabs>
        <Tabs.Screen name="foo" options={{ tabBarLabel: "Foo", tabBarIcon: () => <FontAwesome name="home" /> }} />
        <Tabs.Screen name="bar" options={{ tabBarLabel: "Bar", tabBarIcon: () => <FontAwesome name="gear" /> }} />
      </Tabs>
    </SafeAreaProvider>
  );
}
发布评论

评论列表(0)

  1. 暂无评论