A cross-platform Tab View component for React Native
A cross-platform Tab View component for React Native
The repo has been moved to https://github.com/react-navigation/react-navigation/tree/main/packages/react-native-tab-view. Please open issues and pull requests there.
[![Build Status][build-badge]][build] [![Version][version-badge]][package] [![MIT License][license-badge]][license]
A cross-platform Tab View component for React Native. Implemented using react-native-pager-view on Android & iOS, and PanResponder on Web, macOS, and Windows.
To use this library you need to ensure you are using the correct version of React Native. If you are using a version of React Native that is lower than 0.63 you will need to upgrade that before attempting to use this library.
react-native-tab-view Version |
Required React Native Version |
|---|---|
2.x.x |
< 0.63 |
3.x.x |
>= 0.63 |
Open a Terminal in the project root and run:
yarn add react-native-tab-viewNow we need to install react-native-pager-view if you plan to support iOS and Android.
If you are using Expo, to ensure that you get the compatible versions of the libraries, run:
expo install react-native-pager-viewIf you are not using Expo, run the following:
yarn add react-native-pager-viewWe're done! Now you can build and run the app on your device/simulator.
…The package exports a TabView component which is the one you'd use to render the tab view, and a TabBar component which is the default tab bar implementation.
TabViewContainer component responsible for rendering and managing tabs. Follows material design styles by default.
Basic usage look like this:
<TabView
navigationState={{ index, routes }}
onIndexChange={setIndex}
renderScene={SceneMap({
first: FirstRoute,
second: SecondRoute,
})}
/>navigationState (required)State for the tab view. The state should contain the following properties:
index: a number representing the index of the active route in the routes arrayroutes: an array containing a list of route objects used for rendering the tabsEach route object should contain the following properties:
key: a unique key to identify the route (required)title: title for the route to display in the tab baricon: icon for the route to display in the tab baraccessibilityLabel: accessibility label for the tab buttontestID: test id for the tab buttonExample:
{
index: 1,
routes: [
{ key: 'music', title: 'Music' },
{ key: 'albums', title: 'Albums' },
{ key: 'recents', title: 'Recents' },
{ key: 'purchased', title: 'Purchased' },
]
}TabView is a controlled component, which means the index needs to be updated via the onIndexChange callback.
onIndexChange (required)Callback which is called on tab change, receives the index of the new tab as argument. The navigation state needs to be updated when it's called, otherwise the change is dropped.
renderScene (required)Callback which returns a react element to render as the page for the tab. Receives an object containing the route as the argument:
const renderScene = ({ route, jumpTo }) => {
switch (route.key) {
case 'music':
return <MusicRoute jumpTo={jumpTo} />;
case 'albums':
return <AlbumsRoute jumpTo={jumpTo} />;
}
};You need to make sure that your individual routes implement a shouldComponentUpdate to improve the performance. To make it easier to specify the components, you can use the SceneMap helper.
SceneMap takes an object with the mapping of route.key to React components and returns a function to use with renderScene prop.
import { SceneMap } from 'react-native-tab-view';
...
const renderScene = SceneMap({
music: MusicRoute,
albums: AlbumsRoute,
});Specifying the components this way is easier and takes care of implementing a shouldComponentUpdate method.
Each scene receives the following props:
route: the current route rendered by the componentjumpTo: method to jump to other tabs, takes a route.key as it's argumentposition: animated node which represents the current positionThe jumpTo method can be used to navigate to other tabs programmatically:
this.props.jumpTo('albums');All the scenes rendered with SceneMap are optimized using React.PureComponent and don't re-render when parent's props or states change. If you need more control over how your scenes update (e.g. - triggering a re-render even if the navigationState didn't change), use renderScene directly instead of using SceneMap.
IMPORTANT: Do not pass inline functions to SceneMap, for example, don't do the following:
SceneMap({
first: () => <FirstRoute foo={this.props.foo} />,
second: SecondRoute,
});Always define your components elsewhere in the top level of the file. If you pass inline functions, it'll re-create the component every render, which will cause the entire route to unmount and remount every change. It's very bad for performance and will also cause any local state to be lost.
If you need to pass additional props, use a custom renderScene function:
const renderScene = ({ route }) => {
switch (route.key) {
case 'first':
return <FirstRoute foo={this.props.foo} />;
case 'second':
return <SecondRoute />;
default:
return null;
}
};renderTabBarCallback which returns a custom React Element to use as the tab bar:
import { TabBar } from 'react-native-tab-view';
...
<TabView
renderTabBar={props => <TabBar {...props} />}
...
/>If this is not specified, the default tab bar is rendered. You pass this props to customize the default tab bar, provide your own tab bar, or disable the tab bar completely.
<TabView
renderTabBar={() => null}
...
/>tabBarPositionPosition of the tab bar in the tab view. Possible values are 'top' and 'bottom'. Defaults to 'top'.
lazyFunction which takes an object with the current route and returns a boolean to indicate whether to lazily render the scenes.
By default all scenes are rendered to provide a smoother swipe experience. But you might want to defer the rendering of unfocused scenes until the user sees them. To enable lazy rendering for a particular scene, return true from getLazy for that route:
<TabView
lazy={({ route }) => route.name === 'Albums'}
...
/>When you enable lazy rendering for a screen, it will usually take some time to render when it comes into focus. You can use the renderLazyPlaceholder prop to customize what the user sees during this short period.
You can also pass a boolean to enable lazy for all of the scenes:
<TabView
lazy
/>lazyPreloadDistanceWhen lazy is enabled, you can specify how many adjacent routes should be preloaded with this prop. This value defaults to 0 which means lazy pages are loaded as they come into the viewport.
renderLazyPlaceholderCallback which returns a custom React Element to render for routes that haven't been rendered yet. Receives an object containing the route as the argument. The lazy prop also needs to be enabled.
This view is usually only shown for a split second. Keep it lightweight.
By default, this renders null.
keyboardDismissModeString indicating whether the keyboard gets dismissed in response to a drag gesture. Possible values are:
'auto' (default): the keyboard is dismissed when the index changes.'on-drag': the keyboard is dismissed when a drag begins.'none': drags do not dismiss the keyboard.swipeEnabledBoolean indicating whether to enable swipe gestures. Swipe gestures are enabled by default. Passing false will disable swipe gestures, but the user can still switch tabs by pressing the tab bar.
animationEnabledEnables animation when changing tab. By default it's true.
onSwipeStartCallback which is called when the swipe gesture starts, i.e. the user touches the screen and moves it.
onSwipeEndCallback which is called when the swipe gesture ends, i.e. the user lifts their finger from the screen after the swipe gesture.
initialLayoutObject containing the initial height and width of the screens. Passing this will improve the initial rendering performance. For most apps, this is a good default:
<TabView
initialLayout={{ width: Dimensions.get('window').width }}
...
/>sceneContainerStyleStyle to apply to the view wrapping each screen. You can pass this to override some default styles such as overflow clipping:
pagerStyleStyle to apply to the pager view wrapping all the scenes.
styleStyle to apply to the tab view container.
TabBarMaterial design themed tab bar. To customize the tab bar, you'd need to use the renderTabBar prop of TabView to render the TabBar and pass additional props.
For example, to customize the indicator color and the tab bar background color, you can pass indicatorStyle and style props to the TabBar respectively:
const renderTabBar = props => (
<TabBar
{...props}
indicatorStyle={{ backgroundColor: 'white' }}
style={{ backgroundColor: 'pink' }}
/>
);
//...
return (
<TabView
renderTabBar={renderTabBar}
...
/>
);getLabelTextFunction which takes an object with the current route and returns the label text for the tab. Uses route.title by default.
<TabBar
getLabelText={({ route }) => route.title}
...
/>getAccessibleFunction which takes an object with the current route and returns a boolean to indicate whether to mark a tab as accessible. Defaults to true.
getAccessibilityLabelFunction which takes an object with the current route and returns a accessibility label for the tab button. Uses route.accessibilityLabel by default if specified, otherwise uses the route title.
<TabBar
getAccessibilityLabel={({ route }) => route.accessibilityLabel}
...
/>getTestIDFunction which takes an object with the current route and returns a test id for the tab button to locate this tab button in tests. Uses route.testID by default.
<TabBar
getTestID={({ route }) => route.testID}
...
/>renderIconFunction which takes an object with the current route, focused status and color and returns a custom React Element to be used as a icon.
<TabBar
renderIcon={({ route, focused, color }) => (
<Icon
name={focused ? 'albums' : 'albums-outlined'}
color={color}
/>
)}
...
/>renderLabelFunction whic
No open issues yet, or sync has not completed.