So, extract from https://facebook.github.io/react/docs/more-about-refs.html#the-ref-callback-attribute
The ref attribute can be a callback function instead of a name. This callback will be executed immediately after the component is mounted. The referenced component will be passed in as a parameter, and the callback function may use the component immediately, or save the reference for future use (or both).
It then only gives an example of using the component immediately. I'm trying to find out how i would use this function to access the component immediately, and save the component for future use, as it says we are able to do.
To continue their specific focus()
and theInput
example, how would i call focus()
on the input element, and save it to the theInput
key in refs?
Or put another way, how would i make the console.log
in this fiddle return an object with a theInput
key of the input element's component ref: https://jsfiddle.net/qo3p3fh1/1/
<input type="text" ref={ function(component) { React.findDOMNode( component ).focus(); return 'theInput'; } } />
- no luck – stefvar self = this; <input type="text" ref={ function(component) { self.refs.theInput = component; React.findDOMNode( component ).focus(); } } />
(more out of desperation than anything else - i know the official advice is not to use this.refs inside render functions). still no luck – stef