-
Notifications
You must be signed in to change notification settings - Fork 562
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
RFC: New createRef() API for ref objects #17
Changes from 1 commit
2647d5d
0b0d5d9
ea8070b
c709f3e
7d0d3c4
e249d45
9ae1b73
3bfb605
81a7d52
f9c52ed
a8ccc5a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,7 @@ The primary motivation is to encourage people to migrate off string refs. Callba | |
|
||
There's a few problems with them. | ||
|
||
Strings refs bind to the React's component's `currentOwner` rather than the parent. That's something that isn't statical analysable and leads to most of bugs. | ||
Strings refs bind to the React's component's `currentOwner` rather than the parent. This breaks "render prop" pattern. | ||
|
||
```js | ||
class ComponentA { | ||
|
@@ -49,6 +49,12 @@ class ComponentA { | |
} | ||
``` | ||
|
||
It is not statical analysable and leads to most of bugs. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
? |
||
|
||
It requires that React keeps track of currently rendering component (since it can't guess this). This makes React a bit slower. | ||
|
||
Another problem is breaking with two instances of react if for some reason react packages weren't deduped. | ||
|
||
This alternative API shouldn't provide any big real wins over callback refs - other than being a nice convenience feature. There might be some small wins in performance - as a common pattern is to assign a ref value in a closure created in the render phase of a component - this avoids that (even more so when a ref property is assigned to a non-existent component instance property). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would add here that the primary motivation is to encourage people to migrate off string refs. Callback refs meet some resistance because they are a bit harder to understand. We want to introduce this API primarily for people who love string refs today. |
||
|
||
One benefit would be more correct Flow types. People tend to type refs incorrectly because Flow doesn't enforce uninitialized class properties correctly: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you mention other problems with string refs? For example lack of composability, or that they break “multiple Reacts” case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean by "lack of composability" and how createRef fixes it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And what is the symptom of breaking with multiple reacts. Is there an error or just refs are not filled?