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

Clarify documentation about when onLayout is called

Open evelant opened this issue 5 years ago • 13 comments

Description

https://reactnative.dev/docs/view#onlayout

Trying to animate elements in my app to the screen locations of other elements. I get the position of those elements in their onLayout callbacks and cache the results. In some (unknown, undocumented) situations onLayout is not called when an element changes position.

The current documentation states

Invoked on mount and layout changes with:

{nativeEvent: { layout: {x, y, width, height}}}

This event is fired immediately once the layout has been calculated, but the new layout may not yet be reflected on the screen at the time the event is received, especially if a layout animation is in progress.

This does not specify that in some situations the layout information will be incorrect or transitional. It also does not specify in which situations onLayout will not be called. In my case it appears that onLayout is only called on mount but not when a parent changing size causes an element to change x/y position but remain the same size.

It would be helpful if the documentation for onLayout were expanded to detail exactly in which situations onLayout will be called and in which situations the position/size information may be inaccurate. What exactly constitutes layout changes to fire the event?

edit: here are some related issues detailing the undocumented quirks/bugs with onLayout. https://github.com/facebook/react-native/issues/28775 (animations of transforms don't trigger onLayout) https://github.com/facebook/react-native/issues/3282 (measure returns undefined on android unless onLayout or collapsable are specified) https://github.com/facebook/react-native/issues/24219 (reanimated, standard with expo, changes onLayout behavior) https://github.com/facebook/react-native/issues/23443 (device rotation doesn't trigger onLayout in some situations) https://github.com/facebook/react-native/issues/19872 (onLayout removed elevation on Android)

Documentation version

v0.63

evelant avatar Jul 16 '20 17:07 evelant

👋 Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Dec 25 '20 13:12 stale[bot]

@stale Issue does not fixed

olosegres avatar Jan 21 '21 08:01 olosegres

@Stale Issue does not fixed!

dennisbouwpas avatar Jan 25 '21 18:01 dennisbouwpas

The answer to the following would also be great to document: are x and y relative to the top-left of the whole screen, or to the top-left of the parent? I believe they're relative to the parent, but I don't want to start a false rumor; my investigation might be wrong. 🙂

But the answer is quite important and would be great to have documented.

chrisbobbe avatar Feb 04 '21 21:02 chrisbobbe

+1

lon9man avatar May 12 '21 12:05 lon9man

Has anyone found a solution or a work around to get onlayout to fire when size is changed with animation?

mrcordova avatar Aug 27 '21 19:08 mrcordova

I am trying to find a solution too. Hopefully, that can help https://reactnative.dev/docs/direct-manipulation

audrew avatar Nov 13 '21 02:11 audrew

Don't quote me, but what I'm understanding is that onLayout is only called when that View UI is updated, so if you're changing its position or size in the background but is not reflecting the UI, then onLayout will not get triggered. It may also get called again if the View hits the re-render phrase.

J6ey avatar Jan 22 '22 23:01 J6ey

👋 Hey there, it looks like there has been no activity on this issue in the last 90 days. Has the issue been fixed, or does it still require the community attention? This issue will be closed in the next 7 days if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Apr 23 '22 02:04 github-actions[bot]

Issues don't become invalid just because nobody has bothered to look at them.

evelant avatar Apr 23 '22 02:04 evelant

👋 Hey there, it looks like there has been no activity on this issue in the last 90 days. Has the issue been fixed, or does it still require the community attention? This issue will be closed in the next 7 days if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Jul 23 '22 02:07 github-actions[bot]

Still needs attention

evelant avatar Jul 23 '22 02:07 evelant

Hello, @evelant, did you solve the problem? In my case onLayout gets fired on mount only. Not on translation, rotation, etc.

topheroes avatar Aug 10 '22 11:08 topheroes

👋 Hey there, it looks like there has been no activity on this issue in the last 90 days. Has the issue been fixed, or does it still require the community attention? This issue will be closed in the next 7 days if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Nov 09 '22 02:11 github-actions[bot]

No, the documentation is still unclear

evelant avatar Nov 09 '22 02:11 evelant

👋 Hey there, it looks like there has been no activity on this issue in the last 90 days. Has the issue been fixed, or does it still require the community attention? This issue will be closed in the next 7 days if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Feb 09 '23 02:02 github-actions[bot]

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please feel free to create a new issue with up-to-date information.

github-actions[bot] avatar Feb 16 '23 02:02 github-actions[bot]

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please feel free to create a new issue with up-to-date information.

github-actions[bot] avatar Jul 27 '23 01:07 github-actions[bot]