Skip to content

Commit d746d85

Browse files
committed
fix: prevent updating backdrop state when unmounting (#1657)(by @christophby)
* fix: backdrop -> check if mounted before setting state * chore: updating code styling --------- Co-authored-by: gorhom <gorhom.dev@gmail.com>
1 parent 6203c18 commit d746d85

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/components/bottomSheetBackdrop/BottomSheetBackdrop.tsx

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import React, { memo, useCallback, useMemo, useState } from 'react';
1+
import React, {
2+
memo,
3+
useCallback,
4+
useEffect,
5+
useMemo,
6+
useRef,
7+
useState,
8+
} from 'react';
29
import { ViewProps } from 'react-native';
310
import Animated, {
411
interpolate,
@@ -44,6 +51,7 @@ const BottomSheetBackdropComponent = ({
4451
}: BottomSheetDefaultBackdropProps) => {
4552
//#region hooks
4653
const { snapToIndex, close } = useBottomSheet();
54+
const isMounted = useRef(false);
4755
//#endregion
4856

4957
//#region defaults
@@ -75,7 +83,8 @@ const BottomSheetBackdropComponent = ({
7583
}, [snapToIndex, close, disappearsOnIndex, pressBehavior, onPress]);
7684
const handleContainerTouchability = useCallback(
7785
(shouldDisableTouchability: boolean) => {
78-
setPointerEvents(shouldDisableTouchability ? 'none' : 'auto');
86+
isMounted.current &&
87+
setPointerEvents(shouldDisableTouchability ? 'none' : 'auto');
7988
},
8089
[]
8190
);
@@ -121,6 +130,16 @@ const BottomSheetBackdropComponent = ({
121130
[disappearsOnIndex]
122131
);
123132

133+
// addressing updating the state after unmounting.
134+
// [link](https://github.com/gorhom/react-native-bottom-sheet/issues/1376)
135+
useEffect(() => {
136+
isMounted.current = true;
137+
return () => {
138+
isMounted.current = false;
139+
};
140+
}, []);
141+
//#endregion
142+
124143
const AnimatedView = (
125144
<Animated.View
126145
style={containerStyle}
@@ -139,7 +158,6 @@ const BottomSheetBackdropComponent = ({
139158
{children}
140159
</Animated.View>
141160
);
142-
//#endregion
143161

144162
return pressBehavior !== 'none' ? (
145163
<TapGestureHandler onGestureEvent={gestureHandler}>

0 commit comments

Comments
 (0)