-
Notifications
You must be signed in to change notification settings - Fork 49.2k
Closed
Description
Reason React uses first class OCaml refs to store mutable values (instead of on this
instances).
These are basically just an object with a mutable contents
property. These can be updated with callback refs n => ref.contents = n
but it would be a nice convenience feature to just have that built-in.
We could also make these first class objects on isomorphic React.
React.createRef = () => ({ contents: null });
class Foo extends React.Component {
state = {
myDiv: React.createRef()
};
componentDidMount() {
if (myDiv.contents) {
myDiv.contents.focus();
}
}
render() {
return <div ref={this.state.myDiv} />;
}
}
Basically the implementation would just be:
if (typeof ref === 'function') {
ref(newValue);
} else if (typeof ref === 'object') {
ref.contents = newValue;
} else if (typeof ref === 'string') {
owner.refs[ref] = newValue;
}
This is something that needs to be implemented in the core runtime and not as part of any particular component API since refs cross that boundary.
cc @adamjernst
rickyvetter, glennreyes, FezVrasta, monkindey, cogell and 1 morefaceyspacey