Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onLayout Callback called before ref Callback #911

Closed
ganmor opened this issue Apr 23, 2018 · 7 comments
Closed

onLayout Callback called before ref Callback #911

ganmor opened this issue Apr 23, 2018 · 7 comments

Comments

@ganmor
Copy link

ganmor commented Apr 23, 2018

Do you want to request a feature or report a bug?
It might be considered a bug

What is the current behavior?
The OnLayout Callback is called before the ref callback on component mount.

If the current behavior is a bug, please provide the steps to reproduce and

onLayout() {
   this.someRefAccess() // Does not work
   setTimeout(() => { 
      this.someRefAccess() //Works
  })
}
render() {
  return <View onLayout={this.onLayout} ref={(theref) => { this.someRef = theref; } }> 
}

What is the expected behavior?
On RN native (IOS & Android )
Sequence is
1 ref callback
2 onlayout
It used to be like this on RNWEB

Environment (include versions). Did this work in previous versions?
Confirmed the change affects

  • Firefox 59
  • internet explorer 11

it does not seem to affect chrome

Could it be related to a67bf0f
If the change is intented and we need to handle this manually could you please let us know.

Thank you for the amazing work.

@necolas
Copy link
Owner

necolas commented Apr 23, 2018

Please can you include a link to a test case that reproduces the issue

@necolas
Copy link
Owner

necolas commented Apr 23, 2018

Not sure how it could be possible since React is meant to update the DOM and refs before componentDidMount is called

@ganmor
Copy link
Author

ganmor commented Apr 23, 2018

Here is a minimal test case:

Code:
https://glitch.com/edit/#!/numerous-sprout
Run:
https://numerous-sprout.glitch.me/

It works on Chrome, it does not work on Firefox 59
( Mac OSX High Sierra)
Sorry I do not have access to IE from home.

@necolas
Copy link
Owner

necolas commented Apr 23, 2018

Thanks. The layout handler is here and is only being called in componentDidMount, so I'm not sure why Firefox (with no resize observer support) doesn't have a ref defined.

@necolas
Copy link
Owner

necolas commented Apr 24, 2018

@giuseppeg I'm thinking the resize observer patch might be involved in this issue

@giuseppeg
Copy link
Contributor

giuseppeg commented Apr 25, 2018

@necolas I can take a look later today. fwiw

onLayout={() => { setTimeout(() => { console.log(typeof this.myref) }) }

works on FF. That said I second what you said and I'd expect it to work without scheduling.

@giuseppeg
Copy link
Contributor

giuseppeg commented Apr 25, 2018

@necolas I think we got tricked by the fact that ResizeObserver is async. The fallback behavior is actually the correct one since the OP is attaching a ref on an instance and trying to access it in the render method right away. The correct order is:

Potential View (internal) ref is defined
View#componentDidMount fires <- calls onLayout
App - ref to the View instance is defined
App#componentDidMount fires

https://codepen.io/giuseppegurgone/pen/vjKzyv

We should fix the ResizeObserver case to fire immediately like we do in the fallback case

necolas added a commit that referenced this issue May 8, 2018
onLayout is called synchronously when `ResizeObserver` is being used.

Fix #911
necolas added a commit that referenced this issue May 8, 2018
onLayout is called synchronously when `ResizeObserver` is being used.

Fix #911
@necolas necolas added the has-PR label May 8, 2018
necolas added a commit that referenced this issue May 18, 2018
onLayout is called after the component is mounted to the DOM. This makes
both the fallback and ResizeObserver code path behave the same as React
Native.

Fix #911
Fix #941
Close #939
necolas added a commit that referenced this issue May 18, 2018
onLayout is called after the component is mounted to the DOM. This makes
both the fallback and ResizeObserver code path behave the same as React
Native.

Fix #911
Fix #941
Close #939
necolas added a commit that referenced this issue May 18, 2018
onLayout is called after the component is mounted to the DOM. This makes
both the fallback and ResizeObserver code path behave the same as React
Native.

Fix #911
Fix #941
Close #939
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants