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: closed
- Created 7 years ago
- Reactions: 9
- Comments: 87 (28 by maintainers)
Commits related to this issue
- Ensure header elements are visible on iOS Adds ensureHeaderButtonsVisible method in RNScreens.m References issues #432 and #528. — committed to zibs/react-native-screens by zibs 6 years ago
- fix(iOS): white flash on tab change when using native stack (#2188) ## Description This PR gets rid of undesired white flashes during `maybeAddToParentAndUpdateContainer`. The white flash was pre... — committed to software-mansion/react-native-screens by alduzy 2 years ago
- fix(iOS): white flash on tab change when using native stack (#2188) This PR gets rid of undesired white flashes during `maybeAddToParentAndUpdateContainer`. The white flash was present on paper archi... — committed to software-mansion/react-native-screens by alduzy 2 years ago
- fix(iOS): right header incorrect position (#2316) ## Description This PR fixes the incorrect position of the custom header items when updating more than one option at the same time. The proposed ... — committed to software-mansion/react-native-screens by alduzy 2 years ago
- fix(iOS): white flash on tab change when using native stack (#2188) ## Description This PR gets rid of undesired white flashes during `maybeAddToParentAndUpdateContainer`. The white flash was pre... — committed to WiseOwlTech/react-native-screens by alduzy 2 years ago
- fix(iOS): right header incorrect position (#2316) ## Description This PR fixes the incorrect position of the custom header items when updating more than one option at the same time. The proposed ... — committed to WiseOwlTech/react-native-screens by alduzy 2 years ago
- fix(iOS): right header incorrect position (#2316) ## Description This PR fixes the incorrect position of the custom header items when updating more than one option at the same time. The proposed sol... — committed to software-mansion/react-native-screens by alduzy 2 years ago
I have a workaround if it’s a blocker for anyone here https://github.com/react-navigation/react-navigation/issues/6746#issuecomment-583897436:
Setting the headerTitle in InteractionManager worked for me.
useLayoutEffect(() => { InteractionManager.runAfterInteractions(() => { navigation.setOptions({ headerShown: true, // eslint-disable-next-line react/no-unstable-nested-components headerTitle: () => ( <CustomHeader /> ), }); }); }, [dependency_A]);
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.
Using a custom compontent in headerCenter is affected by the same bug/problem…
(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-routerimplementation and tweaked some of the above advice…Here’s the workaround:
this work for me:
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
24pxwide, so almost half of the button is cropped and not easy to tap.It feels like this extra
20pxon 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?
I found out that the project that @elliottkember and I work on has a default
headerRight: () => <View/>on the stack navigator’s screen options. Removing that and the problem never comes back.In fact the empty view wasn’t the problem per se, it’s the fact that the View expands while, at the same time, the title changes (or back button disappearing). In our case it was going from 0x0 to 20x20, but if you go from 20x20 to 30x20, the same issue happens
One of the cases I can see is using
setOptionsto change theheaderTitleandheaderRightat the same timeWhen x changes from false to true, the
headerRightis shown, but misaligned. It also happens when you remove the back button. I guess basically anything that involves changing the layout while putting on a new header right.If
<HeaderRight>has an internal setTimeout that delays its rendering by a little maybe 100ms, then it’s no longer misaligned. This is v3.34.0@elliottkember Thanks for the message. Having a working example from you would be ideal.
We’re still getting this on v3.34.0 unfortunately. The behaviour has changed slightly – we’re now getting this positioning in a react-navigation drawer header. It happens consistently now, rather than randomly.
We’re also getting this strange “back” button positioning – this isn’t a custom back button but rather a regular back button provided by react-navigation.
This only happens in a route defined at the root of the navigator, above the tab navigator. It’s something to do with not specifying
headerTitlein the header stack. I’ve been trying to make a repo that reproduces this behaviour, but haven’t nailed down exactly what just yet.It may have something to do with our usage of
navigation.setOptions– we use this a little too liberally in our codebase to set navigation options as it’s nice to have navigation options defined with the component’s contents. We’ll refactor that and see whether the behaviour improves.We do use a stack navigator in a drawer – if I initialize that stack with an initial state, the header button is reliably in the wrong place. If I navigate to page 2 with a timeout, it renders in the right place. So something is up with the layout calculation on mount in a drawer. An edge case, but maybe it will help make this
headerRightpositioning more robust.Still no repro repository, I’m afraid, but if we’re able to tame this behaviour I’ll update this comment.
@trajano this should do the thing 👍 thanks!
indeed, the following seems to be working:
@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
RNSScreenviewWillAppear, 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