Skip to content

Commit 817e360

Browse files
TheTektonkelset
authored andcommitted
Avoid one frame delay using TabNavigator (react-navigation#2385)
* Added optional initialLayout func prop to TabNavigatorConfig. * Fix default initialLayout prop type. * Fix default initialLayout prop type. * Fix lint error. * Normalized capitalization and punctuation for TabNavigator.md. * Added react-native-tab-view link to initialLayout prop documentation in TabNavigator.md. * Update TabView.js
1 parent 468a8ca commit 817e360

File tree

3 files changed

+45
-35
lines changed

3 files changed

+45
-35
lines changed

docs/api/navigators/TabNavigator.md

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -81,33 +81,34 @@ The route configs object is a mapping from route name to a route config, which t
8181

8282
### TabNavigatorConfig
8383

84-
- `tabBarComponent` - component to use as the tab bar, e.g. `TabBarBottom`
84+
- `tabBarComponent` - Component to use as the tab bar, e.g. `TabBarBottom`
8585
(this is the default on iOS), `TabBarTop`
86-
(this is the default on Android)
87-
- `tabBarPosition` - position of the tab bar, can be `'top'` or `'bottom'`
88-
- `swipeEnabled` - whether to allow swiping between tabs
89-
- `animationEnabled` - whether to animate when changing tabs
90-
- `lazy` - whether to lazily render tabs as needed as opposed to rendering them upfront
91-
- `tabBarOptions` - configure the tab bar, see below.
86+
(this is the default on Android).
87+
- `tabBarPosition` - Position of the tab bar, can be `'top'` or `'bottom'`.
88+
- `swipeEnabled` - Whether to allow swiping between tabs.
89+
- `animationEnabled` - Whether to animate when changing tabs.
90+
- `lazy` - Whether to lazily render tabs as needed as opposed to rendering them upfront.
91+
- `initialLayout` - Optional object containing the initial `height` and `width`, can be passed to prevent the one frame delay in [react-native-tab-view](https://github.com/react-native-community/react-native-tab-view#avoid-one-frame-delay) rendering.
92+
- `tabBarOptions` - Configure the tab bar, see below.
9293

9394
Several options get passed to the underlying router to modify navigation logic:
9495

95-
- `initialRouteName` - The routeName for the initial tab route when first loading
96-
- `order` - Array of routeNames which defines the order of the tabs
96+
- `initialRouteName` - The routeName for the initial tab route when first loading.
97+
- `order` - Array of routeNames which defines the order of the tabs.
9798
- `paths` - Provide a mapping of routeName to path config, which overrides the paths set in the routeConfigs.
9899
- `backBehavior` - Should the back button cause a tab switch to the initial tab? If yes, set to `initialRoute`, otherwise `none`. Defaults to `initialRoute` behavior.
99100

100101
### `tabBarOptions` for `TabBarBottom` (default tab bar on iOS)
101102

102-
- `activeTintColor` - label and icon color of the active tab
103-
- `activeBackgroundColor` - background color of the active tab
104-
- `inactiveTintColor` - label and icon color of the inactive tab
105-
- `inactiveBackgroundColor` - background color of the inactive tab
106-
- `showLabel` - whether to show label for tab, default is true
107-
- `style` - style object for the tab bar
108-
- `labelStyle` - style object for the tab label
109-
- `tabStyle` - style object for the tab
110-
- `allowFontScaling` - whether label font should scale to respect Text Size accessibility settings, default is true
103+
- `activeTintColor` - Label and icon color of the active tab.
104+
- `activeBackgroundColor` - Background color of the active tab.
105+
- `inactiveTintColor` - Label and icon color of the inactive tab.
106+
- `inactiveBackgroundColor` - Background color of the inactive tab.
107+
- `showLabel` - Whether to show label for tab, default is true.
108+
- `style` - Style object for the tab bar.
109+
- `labelStyle` - Style object for the tab label.
110+
- `tabStyle` - Style object for the tab.
111+
- `allowFontScaling` - Whether label font should scale to respect Text Size accessibility settings, default is true.
111112

112113
Example:
113114

@@ -125,20 +126,20 @@ tabBarOptions: {
125126

126127
### `tabBarOptions` for `TabBarTop` (default tab bar on Android)
127128

128-
- `activeTintColor` - label and icon color of the active tab
129-
- `inactiveTintColor` - label and icon color of the inactive tab
130-
- `showIcon` - whether to show icon for tab, default is false
131-
- `showLabel` - whether to show label for tab, default is true
132-
- `upperCaseLabel` - whether to make label uppercase, default is true
133-
- `pressColor` - color for material ripple (Android >= 5.0 only)
134-
- `pressOpacity` - opacity for pressed tab (iOS and Android < 5.0 only)
135-
- `scrollEnabled` - whether to enable scrollable tabs
136-
- `tabStyle` - style object for the tab
137-
- `indicatorStyle` - style object for the tab indicator (line at the bottom of the tab)
138-
- `labelStyle` - style object for the tab label
139-
- `iconStyle` - style object for the tab icon
140-
- `style` - style object for the tab bar
141-
- `allowFontScaling` - whether label font should scale to respect Text Size accessibility settings, default is true
129+
- `activeTintColor` - Label and icon color of the active tab.
130+
- `inactiveTintColor` - Label and icon color of the inactive tab.
131+
- `showIcon` - Whether to show icon for tab, default is false.
132+
- `showLabel` - Whether to show label for tab, default is true.
133+
- `upperCaseLabel` - Whether to make label uppercase, default is true.
134+
- `pressColor` - Color for material ripple (Android >= 5.0 only).
135+
- `pressOpacity` - Opacity for pressed tab (iOS and Android < 5.0 only).
136+
- `scrollEnabled` - Whether to enable scrollable tabs.
137+
- `tabStyle` - Style object for the tab.
138+
- `indicatorStyle` - Style object for the tab indicator (line at the bottom of the tab).
139+
- `labelStyle` - Style object for the tab label.
140+
- `iconStyle` - Style object for the tab icon.
141+
- `style` - Style object for the tab bar.
142+
- `allowFontScaling` - Whether label font should scale to respect Text Size accessibility settings, default is true.
142143

143144
Example:
144145

@@ -160,15 +161,15 @@ tabBarOptions: {
160161

161162
#### `title`
162163

163-
Generic title that can be used as a fallback for `headerTitle` and `tabBarLabel`
164+
Generic title that can be used as a fallback for `headerTitle` and `tabBarLabel`.
164165

165166
#### `tabBarVisible`
166167

167-
True or false to show or hide the tab bar, if not set then defaults to true
168+
True or false to show or hide the tab bar, if not set then defaults to true.
168169

169170
#### `tabBarIcon`
170171

171-
React Element or a function that given `{ focused: boolean, tintColor: string }` returns a React.Element, to display in tab bar
172+
React Element or a function that given `{ focused: boolean, tintColor: string }` returns a React.Element, to display in tab bar.
172173

173174
#### `tabBarLabel`
174175

src/navigators/TabNavigator.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const TabNavigator = (
3737
swipeEnabled,
3838
animationEnabled,
3939
lazy,
40+
initialLayout,
4041
...tabsConfig
4142
} = mergedConfig;
4243

@@ -56,6 +57,7 @@ const TabNavigator = (
5657
swipeEnabled={swipeEnabled}
5758
animationEnabled={animationEnabled}
5859
lazy={lazy}
60+
initialLayout={initialLayout}
5961
/>
6062
));
6163

@@ -69,13 +71,15 @@ const Presets = {
6971
swipeEnabled: false,
7072
animationEnabled: false,
7173
lazy: false,
74+
initialLayout: undefined,
7275
},
7376
AndroidTopTabs: {
7477
tabBarComponent: TabBarTop,
7578
tabBarPosition: 'top',
7679
swipeEnabled: true,
7780
animationEnabled: true,
7881
lazy: false,
82+
initialLayout: undefined,
7983
},
8084
};
8185

src/views/TabView/TabView.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import React, { PureComponent } from 'react';
44
import { View, StyleSheet } from 'react-native';
55
import { TabViewAnimated, TabViewPagerPan } from 'react-native-tab-view';
6+
import type { Layout } from 'react-native-tab-view/src/TabViewTypeDefinitions';
67
import SceneView from '../SceneView';
78
import withCachedChildNavigation from '../../withCachedChildNavigation';
89

@@ -22,6 +23,7 @@ export type TabViewConfig = {
2223
swipeEnabled?: boolean,
2324
animationEnabled?: boolean,
2425
lazy?: boolean,
26+
initialLayout?: Layout,
2527
};
2628

2729
export type TabScene = {
@@ -38,6 +40,7 @@ type Props = {
3840
swipeEnabled?: boolean,
3941
animationEnabled?: boolean,
4042
lazy?: boolean,
43+
initialLayout?: Layout,
4144

4245
screenProps?: {},
4346
navigation: NavigationScreenProp<NavigationState, NavigationAction>,
@@ -160,6 +163,7 @@ class TabView extends PureComponent<void, Props, void> {
160163
animationEnabled,
161164
swipeEnabled,
162165
lazy,
166+
initialLayout,
163167
screenProps,
164168
} = this.props;
165169

@@ -190,6 +194,7 @@ class TabView extends PureComponent<void, Props, void> {
190194

191195
const props = {
192196
lazy,
197+
initialLayout,
193198
animationEnabled,
194199
swipeEnabled,
195200
renderPager,

0 commit comments

Comments
 (0)