Skip to content

Commit 73e452f

Browse files
authored
Fixed issue with TabView that caused tab screens to recreate every rerender (#671)
1 parent 4d039ac commit 73e452f

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

packages/core/src/components/TabView/TabView.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { StyleProp, ViewStyle } from "react-native";
33
import {
44
TabView,
55
TabBar,
6-
SceneMap,
76
SceneRendererProps,
87
NavigationState,
98
Route,
@@ -15,6 +14,9 @@ import { withTheme } from "../../theming";
1514
import type { Theme } from "../../styles/DefaultTheme";
1615
import { extractStyles } from "../../utilities";
1716

17+
type SceneProps = SceneRendererProps & {
18+
route: Route;
19+
};
1820
type TabBarProps = SceneRendererProps & {
1921
navigationState: NavigationState<any>;
2022
};
@@ -56,7 +58,7 @@ const TabViewComponent: React.FC<React.PropsWithChildren<TabViewProps>> = ({
5658
}) => {
5759
const [index, setIndex] = React.useState(0);
5860
const [routes, setRoutes] = React.useState<Route[]>([]);
59-
const [tabScenes, setTabScenes] = React.useState({});
61+
const [tabScenes, setTabScenes] = React.useState<{ [key: string]: any }>({});
6062

6163
const { textStyles, viewStyles } = extractStyles(style);
6264

@@ -71,7 +73,7 @@ const TabViewComponent: React.FC<React.PropsWithChildren<TabViewProps>> = ({
7173
//Populate routes and scenes based on children
7274
React.useEffect(() => {
7375
const newRoutes: Route[] = [];
74-
const scenes: any = {};
76+
const scenes: { [key: string]: React.ReactElement } = {};
7577

7678
React.Children.toArray(children)
7779
.filter(
@@ -86,7 +88,7 @@ const TabViewComponent: React.FC<React.PropsWithChildren<TabViewProps>> = ({
8688
icon: child.props.icon,
8789
accessibilityLabel: child.props.accessibilityLabel,
8890
});
89-
scenes[idx] = () => child;
91+
scenes[idx] = child;
9092
});
9193

9294
setRoutes(newRoutes);
@@ -125,16 +127,20 @@ const TabViewComponent: React.FC<React.PropsWithChildren<TabViewProps>> = ({
125127
);
126128
};
127129

130+
const renderScene = ({ route }: SceneProps) => {
131+
return tabScenes[route.key];
132+
};
133+
128134
//Cannot render TabView without at least one tab
129135
if (!routes.length) {
130136
return <></>;
131137
}
132138

133139
return (
134140
<TabView
135-
style={[viewStyles]}
141+
style={viewStyles}
136142
navigationState={{ index, routes }}
137-
renderScene={SceneMap(tabScenes)}
143+
renderScene={renderScene}
138144
renderTabBar={renderTabBar}
139145
onIndexChange={indexChangeHandler}
140146
tabBarPosition={tabBarPosition}

0 commit comments

Comments
 (0)