-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
DOM element props should accept functions #10411
Comments
@CharlesStover Sure, why not. The Portal component already supports it:
You can avoid the ref rerender by providing the same function reference. -<span ref={(ref) => { this.spanRef = ref; }} />
+<span ref={this.refHandler} /> |
How do you mean? The re-render has to occur in order for Popover to receive the new anchorEl prop, no? It was previously undefined and now it is that |
Like this, it's documented on React side. |
The point isn't that ref is re-rendering, it's that ref has to re-render in order to get the new This will not work unless a re-render takes place, to my knowledge: <span ref={this.setSpanRef} />
<Popover anchorEl={this.spanRef} /> This will work regardless of a re-render, if Popover supports functions: <span ref={this.setSpanRef} />
<Popover anchorEl={this.getSpanRef} /> |
For example the
Popover
component accepts ananchorEl
prop which points to the DOM element to which to bind the popover.However, if you are grabbing the DOM element with React's
ref
property, you must set a state or otherwise trigger re-render in order to get this DOM element to pass toPopover
.Triggering the re-render often re-triggers the ref, re-triggering the re-render, and repeat and repeat and repeat.
Importantly, nothing in the DOM is changing, so a re-render should not be necessary.
I feel like it would be much more efficient to not bother forcing a re-render if you could pass a function that returns the DOM element to
anchorEl
.Expected Behavior
Not to have to trigger a re-render in order to pass a
ref
DOM element as a property.Current Behavior
You have to trigger a re-render in order to pass a
ref
DOM element as a property.Steps to Reproduce (for bugs)
Alternative
Now whenever Popover needs to access anchorEl, it just calls
this.props.anchorEl()
instead ofthis.props.anchorEl
.It is also simple to check which type, a la:
typeof this.props.anchorEl === 'function' ? this.props.anchorEl() : this.props.anchorEl
The text was updated successfully, but these errors were encountered: