Skip to content

Commit b1e279d

Browse files
fix: avoid rendering provider when no sheet has been opened
1 parent 0ba4f57 commit b1e279d

File tree

1 file changed

+44
-38
lines changed

1 file changed

+44
-38
lines changed

src/BottomSheetView.tsx

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -95,46 +95,52 @@ export function BottomSheetView({ state, descriptors }: Props) {
9595
[colors.border],
9696
);
9797

98-
return (
99-
<BottomSheetModalProvider>
100-
{state.routes.map((route, i) => {
101-
const { options, navigation, render } = descriptors[route.key];
102-
103-
// Top route is the content the bottom sheet is going to be rendered over.
104-
if (i === 0) {
105-
return render();
106-
}
98+
// Avoid rendering provider if we only have one screen.
99+
const shouldRenderProvider = React.useRef(false);
100+
shouldRenderProvider.current =
101+
shouldRenderProvider.current || state.routes.length > 1;
107102

108-
const {
109-
index,
110-
backgroundStyle,
111-
handleIndicatorStyle,
112-
snapPoints = DEFAULT_SNAP_POINTS,
113-
...sheetProps
114-
} = options;
103+
const firstScreen = descriptors[state.routes[0].key];
104+
return (
105+
<>
106+
{firstScreen.render()}
107+
{shouldRenderProvider.current && (
108+
<BottomSheetModalProvider>
109+
{state.routes.slice(1).map((route) => {
110+
const { options, navigation, render } = descriptors[route.key];
115111

116-
return (
117-
<BottomSheetModalScreen
118-
key={route.key}
119-
// Make sure index is in range, it could be out if snapToIndex is persisted
120-
// and snapPoints is changed.
121-
index={Math.min(
122-
route.snapToIndex ?? index ?? 0,
123-
snapPoints.length - 1,
124-
)}
125-
snapPoints={snapPoints}
126-
navigation={navigation}
127-
backgroundStyle={[themeBackgroundStyle, backgroundStyle]}
128-
handleIndicatorStyle={[
129-
themeHandleIndicatorStyle,
112+
const {
113+
index,
114+
backgroundStyle,
130115
handleIndicatorStyle,
131-
]}
132-
{...sheetProps}
133-
>
134-
{render()}
135-
</BottomSheetModalScreen>
136-
);
137-
})}
138-
</BottomSheetModalProvider>
116+
snapPoints = DEFAULT_SNAP_POINTS,
117+
...sheetProps
118+
} = options;
119+
120+
return (
121+
<BottomSheetModalScreen
122+
key={route.key}
123+
// Make sure index is in range, it could be out if snapToIndex is persisted
124+
// and snapPoints is changed.
125+
index={Math.min(
126+
route.snapToIndex ?? index ?? 0,
127+
snapPoints.length - 1,
128+
)}
129+
snapPoints={snapPoints}
130+
navigation={navigation}
131+
backgroundStyle={[themeBackgroundStyle, backgroundStyle]}
132+
handleIndicatorStyle={[
133+
themeHandleIndicatorStyle,
134+
handleIndicatorStyle,
135+
]}
136+
{...sheetProps}
137+
>
138+
{render()}
139+
</BottomSheetModalScreen>
140+
);
141+
})}
142+
</BottomSheetModalProvider>
143+
)}
144+
</>
139145
);
140146
}

0 commit comments

Comments
 (0)