|
2 | 2 |
|
3 | 3 | If you don't use React Navigation, you can use the `TabView` component directly. |
4 | 4 |
|
| 5 | +## Basic Example |
| 6 | + |
5 | 7 | ```tsx |
6 | | -import TabView, { SceneMap } from 'react-native-bottom-tabs'; |
| 8 | +import * as React from 'react'; |
| 9 | +import { View, Text } from 'react-native'; |
| 10 | +import TabView from 'react-native-bottom-tabs'; |
| 11 | + |
| 12 | +const HomeScreen = () => ( |
| 13 | + <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> |
| 14 | + <Text>Home Screen</Text> |
| 15 | + </View> |
| 16 | +); |
| 17 | + |
| 18 | +const SettingsScreen = () => ( |
| 19 | + <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> |
| 20 | + <Text>Settings Screen</Text> |
| 21 | + </View> |
| 22 | +); |
7 | 23 |
|
8 | | -export default function ThreeTabs() { |
9 | | - const [index, setIndex] = useState(0); |
10 | | - const [routes] = useState([ |
11 | | - { key: 'article', title: 'Article', focusedIcon: require('article.png'), badge: '!' }, |
| 24 | +const renderScene = SceneMap({ |
| 25 | + home: HomeScreen, |
| 26 | + settings: SettingsScreen, |
| 27 | +}); |
| 28 | + |
| 29 | +export default function TabViewExample() { |
| 30 | + const [index, setIndex] = React.useState(0); |
| 31 | + const [routes] = React.useState([ |
12 | 32 | { |
13 | | - key: 'albums', |
14 | | - title: 'Albums', |
15 | | - focusedIcon: require('grid.png'), |
16 | | - badge: '5', |
| 33 | + key: 'home', |
| 34 | + title: 'Home', |
| 35 | + focusedIcon: { sfSymbol: 'house' } |
| 36 | + }, |
| 37 | + { |
| 38 | + key: 'settings', |
| 39 | + title: 'Settings', |
| 40 | + focusedIcon: { sfSymbol: 'gear' } |
17 | 41 | }, |
18 | | - { key: 'contacts', title: 'Contacts', focusedIcon: require('person.png') }, |
19 | 42 | ]); |
20 | 43 |
|
21 | | - const renderScene = SceneMap({ |
22 | | - article: Article, |
23 | | - albums: Albums, |
24 | | - contacts: Contacts, |
25 | | - }); |
26 | | - |
27 | 44 | return ( |
28 | 45 | <TabView |
29 | 46 | navigationState={{ index, routes }} |
30 | | - onIndexChange={setIndex} |
31 | 47 | renderScene={renderScene} |
| 48 | + onIndexChange={setIndex} |
| 49 | + labeled |
32 | 50 | /> |
33 | 51 | ); |
34 | 52 | } |
35 | 53 | ``` |
| 54 | + |
| 55 | +## Scene Components |
| 56 | + |
| 57 | +Each scene in the tab view is a separate component that represents a screen. You can define these components independently: |
| 58 | + |
| 59 | +```tsx |
| 60 | +// HomeScreen.tsx |
| 61 | +export const HomeScreen = () => ( |
| 62 | + <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> |
| 63 | + <Text>Home Screen</Text> |
| 64 | + </View> |
| 65 | +); |
| 66 | + |
| 67 | +// SettingsScreen.tsx |
| 68 | +export const SettingsScreen = () => ( |
| 69 | + <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> |
| 70 | + <Text>Settings Screen</Text> |
| 71 | + </View> |
| 72 | +); |
| 73 | +``` |
| 74 | + |
| 75 | +Then use `SceneMap` to map route keys to scene components: |
| 76 | + |
| 77 | +```tsx |
| 78 | +import { SceneMap } from 'react-native-bottom-tabs'; |
| 79 | + |
| 80 | +const renderScene = SceneMap({ |
| 81 | + home: HomeScreen, |
| 82 | + settings: SettingsScreen, |
| 83 | +}); |
| 84 | +``` |
| 85 | + |
| 86 | +## Props |
| 87 | + |
| 88 | +### Required Props |
| 89 | + |
| 90 | +#### `navigationState` |
| 91 | + |
| 92 | +State for the tab view. The state should contain: |
| 93 | +- `routes`: Array of route objects containing `key` and `title` props |
| 94 | +- `index`: Current selected tab index |
| 95 | + |
| 96 | +#### `renderScene` |
| 97 | + |
| 98 | +Function that returns a React element to render for the screen. Can be created using `SceneMap` or custom render function. |
| 99 | + |
| 100 | +#### `onIndexChange` |
| 101 | + |
| 102 | +Callback that is called when the tab index changes. |
| 103 | + |
| 104 | +### Optional Props |
| 105 | + |
| 106 | +#### `labeled` |
| 107 | + |
| 108 | +Whether to show labels in tabs. When `false`, only icons will be displayed. |
| 109 | +- Type: `boolean` |
| 110 | +- Default: `true` |
| 111 | + |
| 112 | +#### `sidebarAdaptable` <Badge text="iOS" type="info" /> |
| 113 | + |
| 114 | +A tab bar style that adapts to each platform: |
| 115 | +- iPadOS: Top tab bar that can adapt into a sidebar |
| 116 | +- iOS: Bottom tab bar |
| 117 | +- macOS/tvOS: Sidebar |
| 118 | +- visionOS: Ornament with sidebar for secondary tabs |
| 119 | + |
| 120 | +#### `ignoresTopSafeArea` <Badge text="iOS" type="info" /> |
| 121 | + |
| 122 | +Whether to ignore the top safe area. |
| 123 | +- Type: `boolean` |
| 124 | + |
| 125 | +#### `disablePageAnimations` <Badge text="iOS" type="info" /> |
| 126 | + |
| 127 | +Whether to disable animations between tabs. |
| 128 | +- Type: `boolean` |
| 129 | + |
| 130 | +#### `hapticFeedbackEnabled` |
| 131 | + |
| 132 | +Whether to enable haptic feedback on tab press. |
| 133 | +- Type: `boolean` |
| 134 | +- Default: `true` |
| 135 | + |
| 136 | +#### `scrollEdgeAppearance` <Badge text="iOS" type="info" /> |
| 137 | + |
| 138 | +Appearance attributes for the tab bar when a scroll view is at the bottom. |
| 139 | +- Type: `'default' | 'opaque' | 'transparent'` |
| 140 | + |
| 141 | +#### `tabBarActiveTintColor` |
| 142 | + |
| 143 | +Color for the active tab. |
| 144 | +- Type: `ColorValue` |
| 145 | + |
| 146 | +#### `tabBarInactiveTintColor` |
| 147 | + |
| 148 | +Color for inactive tabs. |
| 149 | +- Type: `ColorValue` |
| 150 | + |
| 151 | +#### `barTintColor` |
| 152 | + |
| 153 | +Background color of the tab bar. |
| 154 | +- Type: `ColorValue` |
| 155 | + |
| 156 | +#### `translucent` <Badge text="iOS" type="info" /> |
| 157 | + |
| 158 | +Whether the tab bar is translucent. |
| 159 | +- Type: `boolean` |
| 160 | + |
| 161 | +#### `activeIndicatorColor` <Badge text="Android" type="info" /> |
| 162 | + |
| 163 | +Color of tab indicator. |
| 164 | +- Type: `ColorValue` |
| 165 | + |
| 166 | +### Route Configuration |
| 167 | + |
| 168 | +Each route in the `routes` array can have the following properties: |
| 169 | + |
| 170 | +- `key`: Unique identifier for the route |
| 171 | +- `title`: Display title for the tab |
| 172 | +- `focusedIcon`: Icon to show when tab is active |
| 173 | +- `unfocusedIcon`: Icon to show when tab is inactive (optional) |
| 174 | +- `badge`: Badge text to display on the tab |
| 175 | +- `activeTintColor`: Custom active tint color for this specific tab |
| 176 | +- `lazy`: Whether to lazy load this tab's content |
| 177 | + |
| 178 | +### Helper Props |
| 179 | + |
| 180 | +#### `getLazy` |
| 181 | + |
| 182 | +Function to determine if a screen should be lazy loaded. |
| 183 | +- Default: Uses `route.lazy` |
| 184 | + |
| 185 | +#### `getLabelText` |
| 186 | + |
| 187 | +Function to get the label text for a tab. |
| 188 | +- Default: Uses `route.title` |
| 189 | + |
| 190 | +#### `getBadge` |
| 191 | + |
| 192 | +Function to get the badge text for a tab. |
| 193 | +- Default: Uses `route.badge` |
| 194 | + |
| 195 | +#### `getActiveTintColor` |
| 196 | + |
| 197 | +Function to get the active tint color for a tab. |
| 198 | +- Default: Uses `route.activeTintColor` |
| 199 | + |
| 200 | +#### `getIcon` |
| 201 | + |
| 202 | +Function to get the icon for a tab. |
| 203 | +- Default: Uses `route.focusedIcon` and `route.unfocusedIcon` |
0 commit comments