react-native-bottom-sheet: [Bug]: Bottom sheet does not always open when React's Strict Mode is enabled

Version

v5

Reanimated Version

v3

Gesture Handler Version

v2

Platforms

iOS, Android

What happened?

When React’s Strict Mode is enabled, the bottom sheet does not always open.

This is unrelated – they are thrown at the same time but do not cause the issue described here. ~The following errors are also thrown, which may be related (more details in https://github.com/software-mansion/react-native-reanimated/issues/6997):~

ERROR  Warning: findHostInstance_DEPRECATED is deprecated in StrictMode. findHostInstance_DEPRECATED
       was passed an instance of AnimatedComponent(Wrap) which is inside StrictMode. Instead, add a
       ref directly to the element you want to reference. Learn more about using refs safely here:
       https://react.dev/link/strict-mode-find-node
ERROR  Warning: findNodeHandle is deprecated in StrictMode. findNodeHandle was passed an instance of
       Wrap which is inside StrictMode. Instead, add a ref directly to the element you want to reference.
       Learn more about using refs safely here: https://react.dev/link/strict-mode-find-node

https://github.com/user-attachments/assets/676abe68-94c2-43a8-91ab-c8ce32eba26b

Reproduction steps

Using reproduction repository

  1. Clone the repository.
  2. Install dependencies (yarn install).
  3. Build and run the app (e.g. yarn ios).
  4. Press on button to open bottom sheet modal.

The bottom sheet modal does not open (most of the time). findHostInstance_DEPRECATED is deprecated in StrictMode and findNodeHandle is deprecated in StrictMode errors are reported.

Self-reproduction

Enable React’s Strict Mode (wrap app components with <StrictMode>...</StrictMode>).

Reproduction sample

https://github.com/winghouchan/react-native-bottom-sheet-strict-mode-mcve

Ignore as Snack is broken, see #2131, but issue needs it other it will be auto-closed

https://snack.expo.dev/@gorhom/bottom-sheet---issue-reproduction-template

Relevant log output

 LOG  [BottomSheetContainer::handleContainerLayout] height:852
 LOG  [handlePresent] 
 LOG  [handlePortalOnUnmount] minimized:false forcedDismissed:false
 ERROR  Warning: findHostInstance_DEPRECATED is deprecated in StrictMode. findHostInstance_DEPRECATED was passed an instance of AnimatedComponent(Wrap) which is inside StrictMode. Instead, add a ref directly to the element you want to reference. Learn more about using refs safely here: https://react.dev/link/strict-mode-find-node
 ERROR  Warning: findNodeHandle is deprecated in StrictMode. findNodeHandle was passed an instance of Wrap which is inside StrictMode. Instead, add a ref directly to the element you want to reference. Learn more about using refs safely here: https://react.dev/link/strict-mode-find-node
 LOG  [BottomSheetView::handleLayout] height:426
 LOG  [BottomSheetHandleContainer::handleContainerLayout] height:24

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 2
  • Comments: 15 (2 by maintainers)

Commits related to this issue

Most upvoted comments

thanks for diving into this, i’ll look into it shortly

In my previous comment, I said:

In the case of Strict Mode enabled, it looks like animatedHandleHeight doesn’t get its new value from the BottomSheetHandleContainer but retains the INITIAL_HANDLE_HEIGHT of -999 which causes isHandleHeightCalculated to remain false and therefore isLayoutCalculated to be false.

I’ve found this is only partially true. What is true is animatedHandleHeight being the INITIAL_HANDLE_HEIGHT (-999) which causes isHandleHeightCalculated to remain false and therefore isLayoutCalculated to be false. However, it’s not because the value doesn’t change.

I added some more logs to reveal more of what is going on. Here are the logs when Strict Mode is disabled:

[handleMountSheet]
[handlePresent] 
[Portal::handleOnMount]
[useReactiveSharedValue] name:animatedHandleHeight value:-999
[useAnimatedSnapPoints::normalizedSnapPoints] snapPoints:50% containerHeight:852 handleHeight:-999 contentHeight:-999 footerHeight:0 enableDynamicSizing:false maxDynamicContentSize:undefined dynamicSnapPointIndex:-1
[useAnimatedSnapPoints::hasDynamicSnapPoint] snapPoints:50% containerHeight:852 contentHeight:-999 handleHeight:-999 footerHeight:0 enableDynamicSizing:false maxDynamicContentSize:undefined
[BottomSheet::isLayoutCalculated] isContainerHeightCalculated:true isHandleHeightCalculated:false isSnapPointsNormalized:true
[BottomSheetView::handleLayout] height:426
[BottomSheetHandleContainer::handleContainerLayout] height:24
[useAnimatedSnapPoints::normalizedSnapPoints] snapPoints:50% containerHeight:852 handleHeight:-999 contentHeight:-999 footerHeight:0 enableDynamicSizing:false maxDynamicContentSize:undefined dynamicSnapPointIndex:-1
[useAnimatedSnapPoints::hasDynamicSnapPoint] snapPoints:50% containerHeight:852 contentHeight:-999 handleHeight:-999 footerHeight:0 enableDynamicSizing:false maxDynamicContentSize:undefined
[fun::isLayoutCalculated] isContainerHeightCalculated:true isHandleHeightCalculated:false isSnapPointsNormalized:true
[fun::useAnimatedReaction::MaybeSnapPointChange] result:426 previous:null isLayoutCalculated:false
[useAnimatedSnapPoints::normalizedSnapPoints] snapPoints:50% containerHeight:852 handleHeight:24 contentHeight:-999 footerHeight:0 enableDynamicSizing:false maxDynamicContentSize:undefined dynamicSnapPointIndex:-1
[useAnimatedSnapPoints::hasDynamicSnapPoint] snapPoints:50% containerHeight:852 contentHeight:-999 handleHeight:24 footerHeight:0 enableDynamicSizing:false maxDynamicContentSize:undefined
[fun::isLayoutCalculated] isContainerHeightCalculated:true isHandleHeightCalculated:true isSnapPointsNormalized:true
[fun::useAnimatedReaction::MaybeSnapPointChange] result:426 previous:426 isLayoutCalculated:true
[fun::useAnimatedReaction::OnSnapPointChange] result:426
[fun::bound evaluatePosition_Gorhom_BottomSheetTsx15] animationConfigs:undefined source:5
[fun::bound animateToPosition_Gorhom_BottomSheetTsx12] currentPosition:852 nextPosition:426 source:1
[BottomSheet::handleOnAnimate] toIndex:0 fromIndex:-1
[fun::bound animateToPositionCompleted_Gorhom_BottomSheetTsx11] animatedCurrentIndex:-1 animatedNextPosition:426 animatedNextPositionIndex:0
[fun::useAnimatedReaction::OnChange] animatedCurrentIndex:-1 animatedIndex:0
[BottomSheet::handleOnChange] index:0 animatedCurrentIndex:0
[handleBottomSheetOnChange] minimized:false forcedDismissed:false

The log starting with [BottomSheetHandleContainer::handleContainerLayout] is where animatedHandleHeight gets a value set. Note that when deriving the new values in useAnimatedSnapPoints, handleHeight is still -999. It turns out, there’s another pass of deriving the new values in useAnimatedSnapPoints which does have the updated handleHeight. When Strict Mode is enabled the second pass does not occur (the logs stop at the first [fun::useAnimatedReaction::MaybeSnapPointChange]).

I don’t yet fully understand why the first time the values are derived in useAnimatedSnapPoints after animatedHandleHeight is changed does not have the updated animatedHandleHeight and why it requires a second run to get the updated values; or why Strict Mode is affecting this. It may have something to do with how React works, React Native Reanimated doing different pieces of work on different threads and the JS event loop.

It may be an issue that’s the responsibility of React Native Reanimated. I’m trying to create an MCVE that’s only uses React Native Reanimated but haven’t successfully done so and am unsure if I will because I’ve found a workaround which means I’ll be de-prioritising further investigation.

The workaround I’ve found is patching BottomSheetHandleContainer to set the animatedHandleHeight value in a new macro-task by using setTimeout(..., 0):

  const handleContainerLayout = useCallback(
    function handleContainerLayout({
      nativeEvent: {
        layout: { height },
      },
    }: LayoutChangeEvent) {
-      handleHeight.value = height;
+      setTimeout(() => {
+        handleHeight.value = height;
+     }, 0);

      if (__DEV__) {
        print({
          component: BottomSheetHandleContainer.displayName,
          method: 'handleContainerLayout',
          category: 'layout',
          params: {
            height,
          },
        });
      }
    },
    [handleHeight]
  );

Will drop more comments if I continue investigating and find something new. Hope the comments so far have been useful.

@gorhom I’m seeing the same error in the following environment, could you kindly look into this? 🙏

    "expo": "~52.0.42",
    "react": "18.3.1",
    "react-native": "0.76.8",
    "react-native-gesture-handler": "~2.20.2",
    "react-native-reanimated": "~3.16.7",
    "@gorhom/bottom-sheet": "^5.1.2",

Hmm, the setTimeout workaround doesn’t seem to be foolproof. If enableDynamicSizing is true it doesn’t always work (~10% of the time) and a different issue where the sheet takes the height of the screen happens (~30% of the time). Also, attempting to ignore the deprecated API error logs using LogBox.ignoreLogs when enableDynamicSizing is true stops the workaround from working at all 😵‍💫

Found there’s a logger 😄

Here are the logs:

 LOG  [BottomSheetContainer::handleContainerLayout] height:852
 LOG  [handlePresent] 
 LOG  [handlePortalOnUnmount] minimized:false forcedDismissed:false
 ERROR  Warning: findHostInstance_DEPRECATED is deprecated in StrictMode. findHostInstance_DEPRECATED was passed an instance of AnimatedComponent(Wrap) which is inside StrictMode. Instead, add a ref directly to the element you want to reference. Learn more about using refs safely here: https://react.dev/link/strict-mode-find-node
 ERROR  Warning: findNodeHandle is deprecated in StrictMode. findNodeHandle was passed an instance of Wrap which is inside StrictMode. Instead, add a ref directly to the element you want to reference. Learn more about using refs safely here: https://react.dev/link/strict-mode-find-node
 LOG  [BottomSheetView::handleLayout] height:426
 LOG  [BottomSheetHandleContainer::handleContainerLayout] height:24

Interesting that the portal’s unmount handler is run. Suspect this is a consequence of Strict Mode running Effects twice.