-
Notifications
You must be signed in to change notification settings - Fork 224
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
Use forwardRef for all components #548
Labels
breaking-change
A change that will break something for consumers
Comments
7 tasks
Will need to look at this as a bigger milestone across all components. |
just faced similar issue with TextButton. |
All inputs and buttons forward as do new components. The |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
🚀 Feature Proposal
We use an API that allows the spread of extra attributes and properties to a "container" element of a component via
elemProps
. In some cases we even explicitly forward a reference with a naming convention likeinputRef
.elemProps
does not forward refs for us.Assuming
ColorInput
is a class component:React has a
forwardRef
API that allows us to formalize the use ofref
as a property on all components. We would then apply that forwarded ref to the same containing element that extra props are applied to if it makes sense.forwardRef
allows us to keep the implementation ofColorInput
private and allow us to give access to the public interface of the element.Motivation
There are many instances where it is desired to have a reference to an element of a component. We already do this in some cases (e.g.
inputRef
for inputs).ref
can be used on components now, but that just encourages the use of methods or properties of the component. We mostly treat that interface as private and could introduce problems later as we refactor code.Without using
forwardRef
, the implementation details of a component become public. For example, changingColorInput
from a Class Component to a Functional Component becomes a breaking API change.You can always get access to any element on the page using
document.querySelector
. This change would make it easier to get access to the underlying element of each component without resorting to tricks or hacks to do so.Example
Downsides
Another decision for Components - what element should be forwarded? Is it the same element as the element we spread extra
elemProps
over?The text was updated successfully, but these errors were encountered: