Skip to content

Commit 4849a38

Browse files
authored
fix: memoise callbacks passed to TabView (#333)
* fix: memoise callbacks * fix: move SceneMap outside of the component
1 parent b8cbd28 commit 4849a38

File tree

6 files changed

+72
-48
lines changed

6 files changed

+72
-48
lines changed

apps/example/src/Examples/FourTabs.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ interface Props {
1616
activeIndicatorColor?: ColorValue;
1717
}
1818

19+
const renderScene = SceneMap({
20+
article: Article,
21+
albums: Albums,
22+
contacts: Contacts,
23+
chat: Chat,
24+
});
25+
1926
export default function FourTabs({
2027
disablePageAnimations = false,
2128
scrollEdgeAppearance = 'default',
@@ -53,13 +60,6 @@ export default function FourTabs({
5360
},
5461
]);
5562

56-
const renderScene = SceneMap({
57-
article: Article,
58-
albums: Albums,
59-
contacts: Contacts,
60-
chat: Chat,
61-
});
62-
6363
return (
6464
<TabView
6565
sidebarAdaptable

apps/example/src/Examples/Labeled.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ import { Albums } from '../Screens/Albums';
55
import { Contacts } from '../Screens/Contacts';
66
import { Chat } from '../Screens/Chat';
77

8+
const renderScene = SceneMap({
9+
article: Article,
10+
albums: Albums,
11+
contacts: Contacts,
12+
chat: Chat,
13+
});
14+
815
export default function LabeledTabs({
916
showLabels = true,
1017
}: {
@@ -36,13 +43,6 @@ export default function LabeledTabs({
3643
},
3744
]);
3845

39-
const renderScene = SceneMap({
40-
article: Article,
41-
albums: Albums,
42-
contacts: Contacts,
43-
chat: Chat,
44-
});
45-
4646
return (
4747
<TabView
4848
labeled={showLabels}

apps/example/src/Examples/SFSymbols.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ import { Albums } from '../Screens/Albums';
55
import { Contacts } from '../Screens/Contacts';
66
import { Platform } from 'react-native';
77

8+
const renderScene = SceneMap({
9+
article: Article,
10+
albums: Albums,
11+
contacts: Contacts,
12+
});
13+
814
const isAndroid = Platform.OS === 'android';
915

1016
export default function SFSymbols() {
@@ -38,12 +44,6 @@ export default function SFSymbols() {
3844
},
3945
]);
4046

41-
const renderScene = SceneMap({
42-
article: Article,
43-
albums: Albums,
44-
contacts: Contacts,
45-
});
46-
4747
return (
4848
<TabView
4949
sidebarAdaptable

apps/example/src/Examples/ThreeTabs.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import { Article } from '../Screens/Article';
44
import { Albums } from '../Screens/Albums';
55
import { Contacts } from '../Screens/Contacts';
66

7+
const renderScene = SceneMap({
8+
article: Article,
9+
albums: Albums,
10+
contacts: Contacts,
11+
});
12+
713
export default function ThreeTabs() {
814
const [index, setIndex] = useState(1);
915
const [routes] = useState([
@@ -30,12 +36,6 @@ export default function ThreeTabs() {
3036
},
3137
]);
3238

33-
const renderScene = SceneMap({
34-
article: Article,
35-
albums: Albums,
36-
contacts: Contacts,
37-
});
38-
3939
return (
4040
<TabView
4141
navigationState={{ index, routes }}

apps/example/src/Examples/TintColors.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
import * as React from 'react';
21
import TabView, { SceneMap } from 'react-native-bottom-tabs';
32
import { useState } from 'react';
43
import { Article } from '../Screens/Article';
54
import { Albums } from '../Screens/Albums';
65
import { Contacts } from '../Screens/Contacts';
76
import { Chat } from '../Screens/Chat';
87

8+
const renderScene = SceneMap({
9+
article: Article,
10+
albums: Albums,
11+
contacts: Contacts,
12+
chat: Chat,
13+
});
14+
915
export default function TintColorsExample() {
1016
const [index, setIndex] = useState(0);
1117
const [routes] = useState([
@@ -38,13 +44,6 @@ export default function TintColorsExample() {
3844
},
3945
]);
4046

41-
const renderScene = SceneMap({
42-
article: Article,
43-
albums: Albums,
44-
contacts: Contacts,
45-
chat: Chat,
46-
});
47-
4847
return (
4948
<TabView
5049
sidebarAdaptable

packages/react-native-bottom-tabs/src/TabView.tsx

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import React from 'react';
2-
import type { TabViewItems } from './TabViewNativeComponent';
2+
import type {
3+
OnNativeLayout,
4+
OnPageSelectedEventData,
5+
OnTabBarMeasured,
6+
TabViewItems,
7+
} from './TabViewNativeComponent';
38
import {
49
type ColorValue,
510
Image,
@@ -279,6 +284,35 @@ const TabView = <Route extends BaseRoute>({
279284
onIndexChange(index);
280285
});
281286

287+
const handleTabLongPress = React.useCallback(
288+
({ nativeEvent: { key } }: { nativeEvent: OnPageSelectedEventData }) => {
289+
const index = trimmedRoutes.findIndex((route) => route.key === key);
290+
onTabLongPress?.(index);
291+
},
292+
[trimmedRoutes, onTabLongPress]
293+
);
294+
295+
const handlePageSelected = React.useCallback(
296+
({ nativeEvent: { key } }: { nativeEvent: OnPageSelectedEventData }) => {
297+
jumpTo(key);
298+
},
299+
[jumpTo]
300+
);
301+
302+
const handleTabBarMeasured = React.useCallback(
303+
({ nativeEvent: { height } }: { nativeEvent: OnTabBarMeasured }) => {
304+
setTabBarHeight(height);
305+
},
306+
[setTabBarHeight]
307+
);
308+
309+
const handleNativeLayout = React.useCallback(
310+
({ nativeEvent: { width, height } }: { nativeEvent: OnNativeLayout }) => {
311+
setMeasuredDimensions({ width, height });
312+
},
313+
[setMeasuredDimensions]
314+
);
315+
282316
return (
283317
<BottomTabBarHeightContext.Provider value={tabBarHeight}>
284318
<NativeTabView
@@ -290,19 +324,10 @@ const TabView = <Route extends BaseRoute>({
290324
icons={renderCustomTabBar ? undefined : resolvedIconAssets}
291325
selectedPage={focusedKey}
292326
tabBarHidden={!!renderCustomTabBar}
293-
onTabLongPress={({ nativeEvent: { key } }) => {
294-
const index = trimmedRoutes.findIndex((route) => route.key === key);
295-
onTabLongPress?.(index);
296-
}}
297-
onPageSelected={({ nativeEvent: { key } }) => {
298-
jumpTo(key);
299-
}}
300-
onTabBarMeasured={({ nativeEvent: { height } }) => {
301-
setTabBarHeight(height);
302-
}}
303-
onNativeLayout={({ nativeEvent: { width, height } }) => {
304-
setMeasuredDimensions({ width, height });
305-
}}
327+
onTabLongPress={handleTabLongPress}
328+
onPageSelected={handlePageSelected}
329+
onTabBarMeasured={handleTabBarMeasured}
330+
onNativeLayout={handleNativeLayout}
306331
hapticFeedbackEnabled={hapticFeedbackEnabled}
307332
activeTintColor={activeTintColor}
308333
inactiveTintColor={inactiveTintColor}

0 commit comments

Comments
 (0)