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

forceUpdate recalls all ref callbacks #11650

Closed
TrySound opened this issue Nov 24, 2017 · 3 comments
Closed

forceUpdate recalls all ref callbacks #11650

TrySound opened this issue Nov 24, 2017 · 3 comments

Comments

@TrySound
Copy link
Contributor

Do you want to request a feature or report a bug?

Bug, maybe

What is the current behavior?

On calling forceUpdate ref callbacks called again. So, doing forceUpdate in ref callback makes infinite loop.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via https://jsfiddle.net or similar (template for React 16: https://jsfiddle.net/Luktwrdm/, template for React 15: https://jsfiddle.net/hmbg7e9w/).

https://codepen.io/TrySound/pen/mqKqeB?editors=0012

What is the expected behavior?

Do not recall refs

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

react 16.1.1
chrome 62

@gaearon
Copy link
Collaborator

gaearon commented Nov 24, 2017

The fact that refs are called on any updates is expected if you pass an arrow function. I explained why here: #9328 (comment). It is also mentioned in the ref documentation.

The infinite loop itself sounds somewhat expected to me too. You can use ref callbacks for setting fields or setting/clearing native event subscriptions. But unconditionally scheduling additional updates from them sounds like a problem with your code, just like calling forceUpdate in componentDidUpdate would be. What problem are you trying to solve?

@TrySound
Copy link
Contributor Author

@gaearon Got it. Thanks. With function reference it works fine.)
The problem is reacting on dom element existing for react-virtualized WindowScroller
forceUpdate called by https://github.com/andreypopp/react-derivable on atom changes

@gaearon
Copy link
Collaborator

gaearon commented Nov 24, 2017

I don't think I understand the problem from this description. But if your ref callback does anything other than setting a field, it's probably the wrong place to do that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants