Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: backdrop -> check if mounted before setting state #1657

Merged
merged 2 commits into from
Jan 3, 2024

Conversation

christophby
Copy link

@christophby christophby commented Dec 8, 2023

closes #1376 (has already been closed, although it hasn't been fixed yet )

Motivation

When using the backdrop component you could get a "Warning: Can't perform a React state update on an unmounted component [...] in BottomSheetBackdrop" error.

This PR adds a check if the component is mounted before setting the local state to fix that issue.

@gorhom
Copy link
Owner

gorhom commented Jan 1, 2024

i tried to repo the issue but i couldn't, do you have a repo example ?

@christophby
Copy link
Author

@gorhom I don't have a repo example but I just used this component:

BottomSheet.tsx component
import {
  BottomSheetBackdrop,
  BottomSheetBackdropProps,
  BottomSheetModal,
  BottomSheetScrollView,
} from '@gorhom/bottom-sheet';
import React, {useCallback, useRef} from 'react';
import {Button, Text, View} from 'react-native';

interface Props {}

export const BottomSheet = ({}: Props) => {
  const bottomSheetModalRef = useRef<BottomSheetModal>(null);

  const backdropComponent = useCallback(
    (props: BottomSheetBackdropProps) => (
      <BottomSheetBackdrop
        {...props}
        appearsOnIndex={0}
        disappearsOnIndex={-1}
      />
    ),
    [],
  );

  return (
    <View>
      <Button
        title="Open Bottom Sheet"
        onPress={() => {
          bottomSheetModalRef.current?.present();
        }}
      />

      <BottomSheetModal
        ref={bottomSheetModalRef}
        snapPoints={['25%']}
        backdropComponent={backdropComponent}>
        <BottomSheetScrollView>
          <>
            <Text>1 Lorem Ipsum</Text>
            <Text>2 Lorem Ipsum</Text>
          </>
        </BottomSheetScrollView>
      </BottomSheetModal>
    </View>
  );
};

I'm using

"@gorhom/bottom-sheet": "^4.5.1",
"react-native": "0.68.2",
"react-native-gesture-handler": "^2.14.0",
"react-native-reanimated": "^2.8.0",
bottom-sheet-unmount-update.mov

@@ -53,6 +61,16 @@ const BottomSheetBackdropComponent = ({
>(enableTouchThrough ? 'none' : 'auto');
//#endregion

//#region effects
useEffect(() => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you move this hook to the region at the end effects

@gorhom
Copy link
Owner

gorhom commented Jan 3, 2024

looks good, i just left a code styling comment, once it is addressed im going to merge this PR into v4 & v5

@gorhom gorhom self-assigned this Jan 3, 2024
@gorhom gorhom added v4 Written in Reanimated v2 v5 labels Jan 3, 2024
@gorhom gorhom merged commit 8addf04 into gorhom:master Jan 3, 2024
gorhom added a commit that referenced this pull request Jan 3, 2024
…stophby)

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

* chore: updating code styling

---------

Co-authored-by: gorhom <gorhom.dev@gmail.com>
@christophby
Copy link
Author

Thank you for merging

This was referenced Sep 8, 2024
This was referenced Sep 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v4 Written in Reanimated v2 v5
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[v4] State update warning related to BottomSheetBackdrop on closing
2 participants