react-native-reanimated icon indicating copy to clipboard operation
react-native-reanimated copied to clipboard

React Navigation (Native-stack) header hide screen after reanimated layout animation run [only in Android]

Open jittuu opened this issue 3 years ago • 20 comments

Description

After reanimated layout animation runs, react-navigation (native-stack) header hide screen. I'm not sure it is because of react-navigation or reanimated issue, but the app still working fine until reanimated layout animation runs, so I open issue here.

Please check recorded GIF - which can explain error better. 😅

Expected behavior

react-navigation header should not hide screen.

Actual behavior & steps to reproduce

  1. Go to Hello (working fine first time)
  2. Go to Animated
  3. Go to Hello (screen is below header now)

Snack or minimal code example

https://github.com/jittuu/reanimated23-and-nativestack-issue

Package versions

name version
react-native 0.64.3
react-native-reanimated 2.3.1
NodeJS v16.13.1
Xcode 13
Java 15
Gradle
expo 44

Affected platforms

  • [x] Android
  • [ ] iOS
  • [ ] Web

jittuu avatar Jan 31 '22 08:01 jittuu

We have experienced similar issues with Android. Disappearing and duplicating views connected somehow to layout entering/exit transitions.

Great that you have been able to provide minimal example 👍 I assumed it could be screen or absolute position related, because that was common for the affected views.

split avatar Feb 01 '22 15:02 split

I am also experiencing this only on android

edit: even on the 2.4.0

kakajann avatar Feb 05 '22 14:02 kakajann

Same on [email protected]

sashansk3 avatar Feb 16 '22 03:02 sashansk3

I have just run into this exact issue. Runs perfectly on iOS. Removing any entering or exiting animations fixes the problem

alanjhughes avatar Feb 23 '22 07:02 alanjhughes

I'm experiencing something similar in iOS (expo-managed app). If an <Animated.View> with layout prop is somewhere in our render-tree (even a background screen), some views are duplicated or overlap in other screens.

KrasniqiR avatar Feb 23 '22 11:02 KrasniqiR

I had the same issue with native-stack, changed my app to use stack for now until this issue is fixed somehow...

sallar avatar Feb 25 '22 09:02 sallar

also having this when using native-stack, anyone found any solution or workaround?

hisothreed avatar Mar 09 '22 16:03 hisothreed

same issue here

mathiasandresen avatar Mar 23 '22 20:03 mathiasandresen

Can confirm that this is an issue. As soon as Screen A has an Animated.View and no header and you open Screen B that has a header, the content is below the header. As if the header is positioned above it.

Guuz avatar Mar 29 '22 14:03 Guuz

If anyone is interested; this is the workaround we applied:

/**
 * React Native Screens and Reanimated Layout Effects combined result in an issue where the content falls below the native header.
 * To make things worse this issue seems to "fix" itself on Android 10 when the keyboard is opened.
 * This makes it difficult to work around the issue because it's not consistent.
 *
 * As a workaround we force the header to render transparent (position absolute above the content)
 * and add the correct insets as padding. At least this way it works consistently.
 *
 * The issue is tracked here: https://github.com/software-mansion/react-native-reanimated/issues/2906
 */
export default function useApplyHeaderWorkaround(setOptions: (options: NativeStackNavigationOptions) => void) {
  const headerHeight = useHeaderHeight();

  const androidHeaderFix = useMemo(
    () => ({
      headerTransparent: true,
      headerStyle: {backgroundColor: 'white'},
      contentStyle: {paddingTop: headerHeight},
    }),
    [headerHeight],
  );

  React.useLayoutEffect(() => {
    IS_ANDROID && setOptions(androidHeaderFix);
  }, [setOptions, androidHeaderFix]);
}

Which you can use in a Screen component:

useApplyHeaderWorkaround(navigation.setOptions);

Guuz avatar Mar 30 '22 09:03 Guuz

Are there any plans to investigate this?

yotamishak avatar Apr 11 '22 19:04 yotamishak

@yotamishak similar android issues have been reported in #3124, with PR #3157 currently in code review to fix it. Fingers crossed that this issue will be fixed by that PR.

nhanders avatar Apr 12 '22 14:04 nhanders

@yotamishak similar android issues have been reported in #3124, with PR #3157 currently in code review to fix it. Fingers crossed that this issue will be fixed by that PR.

@nhanders I doubt that those issues are related since that PR is for iOS while current issue affected Android. 🤔

jittuu avatar Apr 12 '22 15:04 jittuu

@nhanders

I agree with @jittuu, I just reviewed #3157 and there are only changes in the iOS code. Although they may be similar I don't think it will be resolved in that PR.

yotamishak avatar Apr 12 '22 15:04 yotamishak

This issue still seems to be present on the latest versions of all the packages involved here.

zibs avatar Apr 25 '22 15:04 zibs

This is a huge issue still.. I don't know which repo this is related to but a fix would be nice.

sallar avatar May 05 '22 17:05 sallar

Experiencing the same issue. I confirm that setting the entering / exiting layout animations solve the issue.

andreibarabas avatar May 13 '22 18:05 andreibarabas

Yes, also experiencing the issue. Originally I thought it was react-navigation, but could't repro the issue until the entering animation was added. FYI, I snacked it during my journey: https://snack.expo.dev/@adammcraven/adam-nav-issue

channeladam avatar May 15 '22 08:05 channeladam

I have the same problem when I use @react-navigation/native-stack with @react-navigation/stack it works correctly

Risbot avatar Aug 24 '22 15:08 Risbot

After the error on header screen appears, sometimes my app freezes on navigation then crashes and I have the following error about react-native-screen

java.lang.IndexOutOfBoundsException: Index: 2, Size: 1
    at java.util.ArrayList.add(ArrayList.java:483)
    at com.swmansion.rnscreens.ScreenContainer.addScreen(ScreenContainer.kt:97)
    at com.swmansion.rnscreens.ScreenStackViewManager.addView(ScreenStackViewManager.kt:34)
    at com.swmansion.rnscreens.ScreenStackViewManager.addView(ScreenStackViewManager.kt:16)
    at com.facebook.react.uimanager.NativeViewHierarchyManager.manageChildren(NativeViewHierarchyManager.java:533)
    at com.swmansion.reanimated.layoutReanimation.ReanimatedNativeHierarchyManager.manageChildren(ReanimatedNativeHierarchyManager.java:323)
    at com.facebook.react.uimanager.UIViewOperationQueue$ManageChildrenOperation.execute(UIViewOperationQueue.java:217)

mlecoq avatar Sep 06 '22 12:09 mlecoq

If anyone is interested; this is the workaround we applied:

/**
 * React Native Screens and Reanimated Layout Effects combined result in an issue where the content falls below the native header.
 * To make things worse this issue seems to "fix" itself on Android 10 when the keyboard is opened.
 * This makes it difficult to work around the issue because it's not consistent.
 *
 * As a workaround we force the header to render transparent (position absolute above the content)
 * and add the correct insets as padding. At least this way it works consistently.
 *
 * The issue is tracked here: https://github.com/software-mansion/react-native-reanimated/issues/2906
 */
export default function useApplyHeaderWorkaround(setOptions: (options: NativeStackNavigationOptions) => void) {
  const headerHeight = useHeaderHeight();

  const androidHeaderFix = useMemo(
    () => ({
      headerTransparent: true,
      headerStyle: {backgroundColor: 'white'},
      contentStyle: {paddingTop: headerHeight},
    }),
    [headerHeight],
  );

  React.useLayoutEffect(() => {
    IS_ANDROID && setOptions(androidHeaderFix);
  }, [setOptions, androidHeaderFix]);
}

Which you can use in a Screen component:

useApplyHeaderWorkaround(navigation.setOptions);

I'm using a similar workaround for now, however, it's almost been a year and still this issue exists. For anyone facing this issue who doesn't want to remove layout animations, this sort of fix works. Still though, reanimated needs to always be compatible with react-navigation, I think a lot of people use them together.

JoshBot-Debug avatar Nov 03 '22 03:11 JoshBot-Debug

After nearly 10 months of that issue being here is disqualifing for react-native-reanimated as a library for me, I am removing any instances of it from my project. So many hours that were burned to resolve this issue for me is not a happy time. iOS is working, of course, but to provide good values for my clients I need both platforms to work.

👎

bebe84 avatar Nov 29 '22 13:11 bebe84

I have the same issue, is there any progress resolving this bug?

comertcimen avatar Nov 30 '22 10:11 comertcimen

@bebe84 you are not anybody's client here. It's an open source project. You should be talking about solution here or asking for help. If you have spend many hours on the issue. Please share your findings.

shubhamdeol avatar Dec 01 '22 10:12 shubhamdeol

@shubhamdeol Apologies, thank you for message. A lot of pressure on my shoulders. Yes, findings are more or less the same as ppl wrote above, when you install react-native-reanimated and use @react-navigation/native-stack and use <Animated.View> with layout animations any screen afterwards that have navigation header renders content below it (as if it was absolute).

enableLayoutAnimations(false) fixes it, but if you use layout animations there is no other way as to add paddingTop to every screen that has a header in it

only on Android, iOS is OK

bebe84 avatar Dec 01 '22 10:12 bebe84

Looks like Layout Animations rewrite + https://github.com/software-mansion/react-native-reanimated/pull/3791 will fix this issue. Here are the steps worked for me if someone else wants to give it a try

  1. Install [email protected] or above
  2. Patch it with the changes in https://github.com/software-mansion/react-native-reanimated/pull/3791

FYI, there are also some discussions happening in https://github.com/software-mansion/react-native-reanimated/issues/3368

mk-nickyang avatar Dec 01 '22 14:12 mk-nickyang

@mk-nickyang tried it out. This patch fixes the issue but introduces some other weird issues not sure it's only the case with native stack. Weird issues I noticed

  • icon rendering above the text all of sudden with no change from my side
  • sometimes view never gets removed from on delete of item on exit animation.

Looks like this is not stable fix.

shubhamdeol avatar Dec 01 '22 18:12 shubhamdeol