react-native-screens: headerRight randomly position the component incorrectly
I was testing the createNativeStackNavigation
and ended up having this random issue where the component rendered by headerRight
is being positioned incorrectly sometimes. There’s nothing special in my code, I am just rendering a <Button>
from react-native
.
Here’s the source code: https://github.com/lnmunhoz/react-native-experiments/blob/master/react-navigation-examples/examples/NativeNavigation.tsx.
Update
The issue also happens when the headerLargeTitle
is false
.
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 8
- Comments: 77 (24 by maintainers)
I have a workaround if it’s a blocker for anyone here https://github.com/react-navigation/react-navigation/issues/6746#issuecomment-583897436:
Still happening on
3.25.0
. In my case it happens every time the width of the right component changes due to a button appearing/disappearing. The workaround for me was keeping the width consistent by setting opacity to 0/1.It seems there’s been a bit of a regression here. I am seeing the header title being randomly positioned to the right when using NativeStack from react-navigation. Opening the view debugger in Xcode shows the width as 0, which would explain why it’s “to the right”, it’s centered with the width being 0 and shows the overflow.
(cc @sn-will @trajano @thomasttvo @13Ksii @fukemy @jvfalco1 @elliotfleming @DavidManookyan @nicastelo @FrancescoBonizzi @Johan-dutoit @jadar - bumping everyone that has posted here since 2022)
It’s been years and this still hasn’t been resolved… I found this issue after having trouble with the
expo-router
implementation and tweaked some of the above advice…Here’s the workaround:
this work for me:
Using a custom compontent in headerCenter is affected by the same bug/problem…
I’m also getting this issue on Expo Router ~3.4.7 (SDK 50.0.0). It went away for me when moving more logic about the header into the layout instead of setting it on mount in the useEffect hook. Like this:
Might work for some of you!
Hi @tboba! Unfortunately I couldn’t reproduce it in a deterministic way, and I ended up by making a custom header component, that way I never noticed the issue again!
@WoLewicki It’s on my todo list. Hopefully by Monday next week. Including the other modal bug, if I can reproduce everything.
Closed https://github.com/software-mansion/react-native-screens/issues/571 for this as seems to be related. I shouldn’t be the one to dictate priority but this seems really important, it’s really not ideal to have production apps experience this.
Also I have a feeling mentioning expo is not relevant, I experience this consistently without.
Hey @WoLewicki, do you want me to setup a sample project, showing this issue? Here’s some screenshots demonstrating the problem:
This first one, is just adding a 44x44 Touchable button as the
headerRight
- you can see it’s quite far to the left.If I inspect the element, you can see it’s 44x44:
If I add a
marginRight: -20
, it does “fix” the alignment, but not the issue itself.Now if I inspect the element, you can see it’s only
24px
wide, so almost half of the button is cropped and not easy to tap.It feels like this extra
20px
on the right side added by the native wrapper is causing the problem - if that space would be removed entirely the problem would be solved. The back button works this way if you inspect it:I cloned your project and also made a bare react-native project. The issue exists only while opening it through Expo Client. This means that the version of RNScreens in the current Expo Client is having the bug since it has RNScreens in version 2.0.0-alpha.12. Can you repro it that way to prove if it is right?
@trajano this should do the thing 👍 thanks!
@elliotfleming no need to do that, just put navigation.setOptions inside uselayouteffect
Having the same issue on iPhone.
I only see it happening on release mode and it doesn’t happen always or in all the screens.
Worked it around by setting headerRight as an empty Text on the screen definition.
package.json
@zibs can you post this comment in #528 to keep the discussion there? Also, you can try to move all the code inside this added method to a new method, and then call it in
RNSScreen
viewWillAppear
, maybe it will fix the issue for when coming back from another screen. Please comment if you need more help with applying this.@WoLewicki This 100% fixes my issue!
My replication is very straight forward, just do something along the lines of this:
Then in the screen:
Before
After
This has been on my todo for a few days, I’ll prioritise testing this in the evening @WoLewicki, thanks for letting me know again.
@WoLewicki I’ve been testing #600 in my app (where I could occasionally reproduce the issue) and haven’t seen it since I installed your PR. Haven’t seen any sort of regression either.
So, from my perspective, it looks good!
#600 should be ready now. Can you check if it does not destroy any of your behavior? @kyle-ssg @lnmunhoz