Skip to content

Commit

Permalink
fix: prevent updating backdrop state when unmounting (#1657)(by @chri…
Browse files Browse the repository at this point in the history
…stophby)

* fix: backdrop -> check if mounted before setting state

* chore: updating code styling

---------

Co-authored-by: gorhom <gorhom.dev@gmail.com>
  • Loading branch information
gorhom committed Jan 3, 2024
1 parent 6203c18 commit d746d85
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/components/bottomSheetBackdrop/BottomSheetBackdrop.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import React, { memo, useCallback, useMemo, useState } from 'react';
import React, {
memo,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
import { ViewProps } from 'react-native';
import Animated, {
interpolate,
Expand Down Expand Up @@ -44,6 +51,7 @@ const BottomSheetBackdropComponent = ({
}: BottomSheetDefaultBackdropProps) => {
//#region hooks
const { snapToIndex, close } = useBottomSheet();
const isMounted = useRef(false);
//#endregion

//#region defaults
Expand Down Expand Up @@ -75,7 +83,8 @@ const BottomSheetBackdropComponent = ({
}, [snapToIndex, close, disappearsOnIndex, pressBehavior, onPress]);
const handleContainerTouchability = useCallback(
(shouldDisableTouchability: boolean) => {
setPointerEvents(shouldDisableTouchability ? 'none' : 'auto');
isMounted.current &&
setPointerEvents(shouldDisableTouchability ? 'none' : 'auto');
},
[]
);
Expand Down Expand Up @@ -121,6 +130,16 @@ const BottomSheetBackdropComponent = ({
[disappearsOnIndex]
);

// addressing updating the state after unmounting.
// [link](https://github.com/gorhom/react-native-bottom-sheet/issues/1376)
useEffect(() => {
isMounted.current = true;
return () => {
isMounted.current = false;
};
}, []);
//#endregion

const AnimatedView = (
<Animated.View
style={containerStyle}
Expand All @@ -139,7 +158,6 @@ const BottomSheetBackdropComponent = ({
{children}
</Animated.View>
);
//#endregion

return pressBehavior !== 'none' ? (
<TapGestureHandler onGestureEvent={gestureHandler}>
Expand Down

0 comments on commit d746d85

Please sign in to comment.