Add support for createRef() and react-create-ref#1138
Conversation
| import { rerender } from './render-queue'; | ||
| import options from './options'; | ||
|
|
||
| const createRef = Object; |
There was a problem hiding this comment.
Just curious: Is using Object faster than a literal {}?
There was a problem hiding this comment.
slower. I just benchmarked and while I'd originally thought it'd be worth using Object here, the performance hit is surprising (50%):
https://esbench.com/bench/5b1d4021f2949800a0f61d25

|
Had to read up on the backstory a bit on why react made an the API change. It's basically because of code like this: class Foo extends Component {
frames = [];
render() {
// <div> is passed a new function on each render()
return <div ref={el => {this.frames.push(el)}} />
}
}Overall, it's great to see more work being put into removing wrong usages of an API, but I must admit that I'm a bit worried that this will be the first change of many others where we "memoize" function creation. Spoke a bit with Dan at JSConf about this and the render props pattern which has a similar issue. The React team is currently exploring alternative ideas and would like to make them unnecessary at some point in the future. There are no short term plans for that, though. It just something they are currently exploring. I wouldn't worry too much about supporting @developit Does your second implementation work as well as the changes in this PR? Just tried it out and it would be 5 bytes smaller. |
|
@marvinhagemeister it works pretty similarly. I'm actually seeing a performance improvement with this implementation though, so I might try to benchmark between the two for comparison. The alternative version is technically different from how React implements createRef (since it returns a function with a |
|
@developit Right, thanks for getting back to me. In that case I think we should not deviate too much from |
|
ping @lukeed |
|
Are we thinking the 38 byte hit is okay? |
|
Sorry for the delay, crazy week. The only shavings (lol) I can suggest is Other than that, I think I'm of the mindset that we should become react-16 compatible all at once, when that point comes, rather than piecemeal some 16 attributes. Will be harder for users to keep track of what we currently do / don't do while still intentionally being incomplete. |
| */ | ||
| export function applyRef(ref, value) { | ||
| if (ref!=null) { | ||
| if (typeof ref=='function') ref(value); |
There was a problem hiding this comment.
This line is here to be compatible with the current way refs work, right?
|
I think I tried the |
I tend to disagree. Lack of support for react-16 features could be holding back adoption of Preact due to incompatibility with libraries (eg. reach/router#9). Rolling out support as soon as a feature is ready could help adoption. For example, I'm super keen to use Preact in production but am held back through issues like this. |
|
Merging as everybody is happy with the change 🎉 |
Implemented in preactjs/preact#1138, we just have to export it here. This won't do anything until Preact 8.4.0 is released.
|
Sorry in advance if this is a dumb question. |
|
@gordygordy No, we haven't made a new release yet. If absolutely need the latest unreleased code from |
|
I think I solved it, i just pasted this into the bottom of the preact.min.js file and now everything works :) function createRef() {return function ref(c) { ref.current = c; }} |
|
@gordygordy I'd recommend avoiding modifying anything in |
|
Can the |
|
@MathieuLoutre should work right out of the box with |
|
I've tried it and it seems to work brilliantly. Just wanted to make sure that it was supposed to do that ;) Thank you! |
`ceeateRef` was added in preactjs#1138 however the Flow definition wasn’t added, so using it on Flow codebases was quite inconvenient
Like the title says, this adds support for Object refs - the
createRef()API (returns an empty object). It should also work with thereact-create-refpackage.Worth noting, this is around a 38 byte size increase and puts us up to 3535b, which I'm not thrilled about.
It's possible we could punt support for createRef() into preact-compat, since it can be implemented atop function refs quite easily:
... however that would not be compatible with the
react-create-refpackage, so if we got that route we'll need to provide an alias for it that points to preact-compat's createRef.