-
Notifications
You must be signed in to change notification settings - Fork 47.4k
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
Add React.createRef() as the migration path for string refs #11973
Comments
As an alternative to adding new React API, could this just be moved into userspace? In the OP from #11555, we could amend the example as: import createRef from 'create-react-ref';
class MyComponent extends React.Component {
render() {
return <div><input type="text" ref={createRef(this, 'div')} /></div>;
}
componentDidMount() {
this.refs.div.value.focus();
}
} As a really dumb implementation: export default function createRef(inst, refName) {
if (!inst.refs) {
inst.refs = {};
}
if (!inst.stringRefFuncs) {
inst.stringRefFuncs = {};
}
if (!inst.stringRefFuncs[refName]) {
inst.stringRefFuncs[refName] = c => {
inst.refs[refName] = c;
};
}
return inst.stringRefFuncs[refName];
} This may not literally work as-is given that I think touching It would even be possible to write a Babel transform to do this automatically. |
Alternatively: function createRef() {
function ref(c) {
ref.value = c;
}
return ref;
} I don't know if the edge cases exactly match #11555, but this can live in userspace, and doesn't require any changes to the reconciler. Unless per #11555 (comment) and #11555 (comment), the intention is for users of "vanilla" function refs to migrate to this scheme as well. |
What is the benefit of moving it out? File size? (Presumably it would be small enough and the vast majority of libs would need it anyway.) |
I'm not sure. I didn't quite follow the conclusion from #11555 (comment). Is the idea that, going forward, most ref users will use If it's a convenience thing, it seems like putting this in user space reduces the React API surface area. |
I think the idea is that most people will use |
That makes sense. Thanks! |
Maybe it will be less attractive for use, because it's another dependency? In this case, it's not so obvious what will be preferable for use. That makes sense? |
@strayiker use rfc for comments |
Shipped in 16.3 |
Creating this issue to track #11555. I intend to close the PR as it's outdated, but we probably want to turn it into a real RFC and potentially get it in during 16.x.
The text was updated successfully, but these errors were encountered: