|
| 1 | +--- |
| 2 | +title: Using BottomNavigation with React Navigation |
| 3 | +--- |
| 4 | + |
| 5 | +A material-design themed tab bar on the bottom of the screen that lets you switch between different routes with animation. Routes are lazily initialized - their screen components are not mounted until they are first focused. |
| 6 | + |
| 7 | +This wraps the [`BottomNavigation`](https://callstack.github.io/react-native-paper/docs/components/BottomNavigation/) component from `react-native-paper`, however if you [configure the Babel plugin](https://callstack.github.io/react-native-paper/docs/guides/getting-started/), it won't include the whole library in your bundle. |
| 8 | + |
| 9 | +<img src="/react-native-paper/screenshots/material-bottom-tabs.gif" style={{ width: '420px', maxWidth: '100%', margin: '16px 0' }} /> |
| 10 | + |
| 11 | +:::info |
| 12 | +To use this navigator, ensure that you have [`@react-navigation/native` and its dependencies (follow this guide)](https://reactnavigation.org/docs/getting-started): |
| 13 | +::: |
| 14 | + |
| 15 | +> For a complete example please visit `createMaterialBottomTabNavigator` [snack](https://snack.expo.dev/@react-native-paper/creatematerialbottomtabnavigator) |
| 16 | +
|
| 17 | +## API Definition |
| 18 | + |
| 19 | +To use this tab navigator, import it from `react-native-paper/react-navigation`: |
| 20 | + |
| 21 | +```js |
| 22 | +import { createMaterialBottomTabNavigator } from 'react-native-paper/react-navigation'; |
| 23 | + |
| 24 | +const Tab = createMaterialBottomTabNavigator(); |
| 25 | + |
| 26 | +function MyTabs() { |
| 27 | + return ( |
| 28 | + <Tab.Navigator> |
| 29 | + <Tab.Screen name="Home" component={HomeScreen} /> |
| 30 | + <Tab.Screen name="Settings" component={SettingsScreen} /> |
| 31 | + </Tab.Navigator> |
| 32 | + ); |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +> For a complete usage guide please visit [Tab Navigation](https://reactnavigation.org/docs/tab-based-navigation/) |
| 37 | +
|
| 38 | +### Props |
| 39 | + |
| 40 | +The `Tab.Navigator` component accepts following props: |
| 41 | + |
| 42 | +#### `id` |
| 43 | + |
| 44 | +Optional unique ID for the navigator. This can be used with [`navigation.getParent`](https://reactnavigation.org/docs/navigation-prop#getparent) to refer to this navigator in a child navigator. |
| 45 | + |
| 46 | +#### `initialRouteName` |
| 47 | + |
| 48 | +The name of the route to render on first load of the navigator. |
| 49 | + |
| 50 | +#### `screenOptions` |
| 51 | + |
| 52 | +Default options to use for the screens in the navigator. |
| 53 | + |
| 54 | +#### `backBehavior` |
| 55 | + |
| 56 | +This controls what happens when `goBack` is called in the navigator. This includes pressing the device's back button or back gesture on Android. |
| 57 | + |
| 58 | +It supports the following values: |
| 59 | + |
| 60 | +- `firstRoute` - return to the first screen defined in the navigator (default) |
| 61 | +- `initialRoute` - return to initial screen passed in `initialRouteName` prop, if not passed, defaults to the first screen |
| 62 | +- `order` - return to screen defined before the focused screen |
| 63 | +- `history` - return to last visited screen in the navigator; if the same screen is visited multiple times, the older entries are dropped from the history |
| 64 | +- `none` - do not handle back button |
| 65 | + |
| 66 | +#### `shifting` |
| 67 | + |
| 68 | +Whether the shifting style is used, the active tab icon shifts up to show the label and the inactive tabs won't have a label. |
| 69 | + |
| 70 | +By default, this is `true` when you have more than 3 tabs. Pass `shifting={false}` to explicitly disable this animation, or `shifting={true}` to always use this animation. |
| 71 | + |
| 72 | +#### `labeled` |
| 73 | + |
| 74 | +Whether to show labels in tabs. When `false`, only icons will be displayed. |
| 75 | + |
| 76 | +#### `activeColor` |
| 77 | + |
| 78 | +Custom color for icon and label in the active tab. |
| 79 | + |
| 80 | +#### `inactiveColor` |
| 81 | + |
| 82 | +Custom color for icon and label in the inactive tab. |
| 83 | + |
| 84 | +#### `barStyle` |
| 85 | + |
| 86 | +Style for the bottom navigation bar. You can pass custom background color here: |
| 87 | + |
| 88 | +```js |
| 89 | +<Tab.Navigator |
| 90 | + initialRouteName="Home" |
| 91 | + activeColor="#f0edf6" |
| 92 | + inactiveColor="#3e2465" |
| 93 | + barStyle={{ backgroundColor: '#694fad' }} |
| 94 | +> |
| 95 | + {/* ... */} |
| 96 | +</Tab.Navigator> |
| 97 | +``` |
| 98 | + |
| 99 | +If you have a translucent navigation bar on Android, you can also set a bottom padding here: |
| 100 | + |
| 101 | +```js |
| 102 | +<Tab.Navigator |
| 103 | + initialRouteName="Home" |
| 104 | + activeColor="#f0edf6" |
| 105 | + inactiveColor="#3e2465" |
| 106 | + barStyle={{ paddingBottom: 48 }} |
| 107 | +> |
| 108 | + {/* ... */} |
| 109 | +</Tab.Navigator> |
| 110 | +``` |
| 111 | + |
| 112 | +#### `theme` |
| 113 | + |
| 114 | +Enables the customization of default theme attributes (e.g. colors) or facilitates the utilization of a personalized custom theme. |
| 115 | + |
| 116 | +### Options |
| 117 | + |
| 118 | +The following [options](https://reactnavigation.org/docs/screen-options) can be used to configure the screens in the navigator: |
| 119 | + |
| 120 | +#### `title` |
| 121 | + |
| 122 | +Generic title that can be used as a fallback for `headerTitle` and `tabBarLabel`. |
| 123 | + |
| 124 | +#### `tabBarIcon` |
| 125 | + |
| 126 | +Function that given `{ focused: boolean, color: string }` returns a React.Node, to display in the tab bar. |
| 127 | + |
| 128 | +#### `tabBarColor` |
| 129 | + |
| 130 | +Color for the tab bar when the tab corresponding to the screen is active. Used for the ripple effect. This is only supported when `shifting` is `true`. |
| 131 | + |
| 132 | +#### `tabBarLabel` |
| 133 | + |
| 134 | +Title string of a tab displayed in the tab bar. When undefined, scene `title` is used. To hide, see `labeled` option in the previous section. |
| 135 | + |
| 136 | +#### `tabBarBadge` |
| 137 | + |
| 138 | +Badge to show on the tab icon, can be `true` to show a dot, `string` or `number` to show text. |
| 139 | + |
| 140 | +#### `tabBarAccessibilityLabel` |
| 141 | + |
| 142 | +Accessibility label for the tab button. This is read by the screen reader when the user taps the tab. It's recommended to set this if you don't have a label for the tab. |
| 143 | + |
| 144 | +#### `tabBarTestID` |
| 145 | + |
| 146 | +ID to locate this tab button in tests. |
| 147 | + |
| 148 | +### Events |
| 149 | + |
| 150 | +The navigator can [emit events](https://reactnavigation.org/docs/navigation-events) on certain actions. Supported events are: |
| 151 | + |
| 152 | +#### `tabPress` |
| 153 | + |
| 154 | +This event is fired when the user presses the tab button for the current screen in the tab bar. By default a tab press does several things: |
| 155 | + |
| 156 | +- If the tab is not focused, tab press will focus that tab |
| 157 | +- If the tab is already focused: |
| 158 | + - If the screen for the tab renders a scroll view, you can use [`useScrollToTop`](https://reactnavigation.org/docs/use-scroll-to-top) to scroll it to top |
| 159 | + - If the screen for the tab renders a stack navigator, a `popToTop` action is performed on the stack |
| 160 | + |
| 161 | +To prevent the default behavior, you can call `event.preventDefault`: |
| 162 | + |
| 163 | +```js |
| 164 | +React.useEffect(() => { |
| 165 | + const unsubscribe = navigation.addListener('tabPress', (e) => { |
| 166 | + // Prevent default behavior |
| 167 | + |
| 168 | + e.preventDefault(); |
| 169 | + // Do something manually |
| 170 | + // ... |
| 171 | + }); |
| 172 | + |
| 173 | + return unsubscribe; |
| 174 | +}, [navigation]); |
| 175 | +``` |
| 176 | + |
| 177 | +### Helpers |
| 178 | + |
| 179 | +The tab navigator adds the following methods to the navigation prop: |
| 180 | + |
| 181 | +#### `jumpTo` |
| 182 | + |
| 183 | +Navigates to an existing screen in the tab navigator. The method accepts following arguments: |
| 184 | + |
| 185 | +- `name` - _string_ - Name of the route to jump to. |
| 186 | +- `params` - _object_ - Screen params to pass to the destination route. |
| 187 | + |
| 188 | +<samp id="material-tab-jump-to" /> |
| 189 | + |
| 190 | +```js |
| 191 | +navigation.jumpTo('Profile', { name: 'Michaś' }); |
| 192 | +``` |
| 193 | + |
| 194 | +## Example |
| 195 | + |
| 196 | +```js |
| 197 | +import { createMaterialBottomTabNavigator } from 'react-native-paper/react-navigation'; |
| 198 | +import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons'; |
| 199 | + |
| 200 | +const Tab = createMaterialBottomTabNavigator(); |
| 201 | + |
| 202 | +function MyTabs() { |
| 203 | + return ( |
| 204 | + <Tab.Navigator |
| 205 | + initialRouteName="Feed" |
| 206 | + activeColor="#e91e63" |
| 207 | + barStyle={{ backgroundColor: 'tomato' }} |
| 208 | + > |
| 209 | + <Tab.Screen |
| 210 | + name="Feed" |
| 211 | + component={Feed} |
| 212 | + options={{ |
| 213 | + tabBarLabel: 'Home', |
| 214 | + tabBarIcon: ({ color }) => ( |
| 215 | + <MaterialCommunityIcons name="home" color={color} size={26} /> |
| 216 | + ), |
| 217 | + }} |
| 218 | + /> |
| 219 | + <Tab.Screen |
| 220 | + name="Notifications" |
| 221 | + component={Notifications} |
| 222 | + options={{ |
| 223 | + tabBarLabel: 'Updates', |
| 224 | + tabBarIcon: ({ color }) => ( |
| 225 | + <MaterialCommunityIcons name="bell" color={color} size={26} /> |
| 226 | + ), |
| 227 | + }} |
| 228 | + /> |
| 229 | + <Tab.Screen |
| 230 | + name="Profile" |
| 231 | + component={Profile} |
| 232 | + options={{ |
| 233 | + tabBarLabel: 'Profile', |
| 234 | + tabBarIcon: ({ color }) => ( |
| 235 | + <MaterialCommunityIcons name="account" color={color} size={26} /> |
| 236 | + ), |
| 237 | + }} |
| 238 | + /> |
| 239 | + </Tab.Navigator> |
| 240 | + ); |
| 241 | +} |
| 242 | +``` |
0 commit comments