Skip to content

Commit 5c18f45

Browse files
committed
Update custom tab bar example
1 parent ee9ab9e commit 5c18f45

File tree

2 files changed

+79
-13
lines changed

2 files changed

+79
-13
lines changed

versioned_docs/version-7.x/bottom-tab-navigator.md

Lines changed: 79 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,27 @@ Style object for the component wrapping the screen content.
166166

167167
Function that returns a React element to display as the tab bar.
168168

169-
Example:
169+
The function receives an object containing the following properties as the argument:
170170

171-
<samp id="custom-tab-bar" />
171+
- `state` - The state object for the tab navigator.
172+
- `descriptors` - The descriptors object containing options for the tab navigator.
173+
- `navigation` - The navigation object for the tab navigator.
172174

173-
```js
175+
The `state.routes` array contains all the routes defined in the navigator. Each route's options can be accessed using `descriptors[route.key].options`.
176+
177+
Example:
178+
179+
```js name="Custom tab bar" snack tabs=config
180+
import * as React from 'react';
181+
import {
182+
createStaticNavigation,
183+
NavigationContainer,
184+
} from '@react-navigation/native';
174185
// codeblock-focus-start
175-
import { View, Text, TouchableOpacity, Platform } from 'react-native';
186+
import { View, Platform } from 'react-native';
176187
import { useLinkBuilder, useTheme } from '@react-navigation/native';
188+
import { Text, PlatformPressable } from '@react-navigation/elements';
189+
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
177190

178191
function MyTabBar({ state, descriptors, navigation }) {
179192
const { colors } = useTheme();
@@ -212,9 +225,8 @@ function MyTabBar({ state, descriptors, navigation }) {
212225
};
213226

214227
return (
215-
<TouchableOpacity
228+
<PlatformPressable
216229
href={buildHref(route.name, route.params)}
217-
accessibilityRole={Platform.OS === 'web' ? 'link' : 'button'}
218230
accessibilityState={isFocused ? { selected: true } : {}}
219231
accessibilityLabel={options.tabBarAccessibilityLabel}
220232
testID={options.tabBarButtonTestID}
@@ -225,19 +237,75 @@ function MyTabBar({ state, descriptors, navigation }) {
225237
<Text style={{ color: isFocused ? colors.primary : colors.text }}>
226238
{label}
227239
</Text>
228-
</TouchableOpacity>
240+
</PlatformPressable>
229241
);
230242
})}
231243
</View>
232244
);
233245
}
246+
234247
// codeblock-focus-end
235248

236-
// ...
249+
function HomeScreen() {
250+
return (
251+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
252+
<Text>Home Screen</Text>
253+
</View>
254+
);
255+
}
256+
257+
function ProfileScreen() {
258+
return (
259+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
260+
<Text>Profile Screen</Text>
261+
</View>
262+
);
263+
}
237264

238-
<Tab.Navigator tabBar={(props) => <MyTabBar {...props} />}>
239-
{/* ... */}
240-
</Tab.Navigator>;
265+
// codeblock-tabs=static
266+
// codeblock-focus-start
267+
const MyTabs = createBottomTabNavigator({
268+
// highlight-next-line
269+
tabBar: (props) => <MyTabBar {...props} />,
270+
screens: {
271+
Home: HomeScreen,
272+
Profile: ProfileScreen,
273+
},
274+
});
275+
// codeblock-focus-end
276+
277+
const Navigation = createStaticNavigation(MyTabs);
278+
279+
export default function App() {
280+
return <Navigation />;
281+
}
282+
// codeblock-tabs-end
283+
284+
// codeblock-tabs=dynamic
285+
const Tab = createBottomTabNavigator();
286+
287+
// codeblock-focus-start
288+
function MyTabs() {
289+
return (
290+
<Tab.Navigator
291+
// highlight-next-line
292+
tabBar={(props) => <MyTabBar {...props} />}
293+
>
294+
<Tab.Screen name="Home" component={HomeScreen} />
295+
<Tab.Screen name="Profile" component={ProfileScreen} />
296+
</Tab.Navigator>
297+
);
298+
}
299+
// codeblock-focus-end
300+
301+
export default function App() {
302+
return (
303+
<NavigationContainer>
304+
<MyTabs />
305+
</NavigationContainer>
306+
);
307+
}
308+
// codeblock-tabs-end
241309
```
242310

243311
This example will render a basic tab bar with labels.

versioned_docs/version-7.x/params.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,6 @@ You can also pass some initial params to a screen. If you didn't specify any par
125125
</TabItem>
126126
<TabItem value="dynamic" label="Dynamic">
127127

128-
<samp id="initial-params" />
129-
130128
```js
131129
<Stack.Screen
132130
name="Details"

0 commit comments

Comments
 (0)