Callback Refs

React also supports another way to set refs called “callback refs”, which gives more fine-grain control over when refs are set and unset.

Instead of passing a ref attribute created by createRef(), you pass a function. The function receives the React component instance or HTML DOM element as its argument, which can be stored and accessed elsewhere.

The example below implements a common pattern: using the ref callback to store a reference to a DOM node in an instance property.

class CustomTextInput extends React.Component {

constructor(props) {

super(props);

this.textInput = null;

this.setTextInputRef = element => {

this.textInput = element;

};

this.focusTextInput = () => {

// Focus the text input using the raw DOM API

if (this.textInput) this.textInput.focus();

};

}

componentDidMount() {

// autofocus the input on mount

this.focusTextInput();

}

render() {

// Use the `ref` callback to store a reference to the text input DOM

// element in an instance field (for example, this.textInput).

return (

<div>

<input

type=”text”

ref={this.setTextInputRef}

/>

<input

type=”button”

value=”Focus the text input”

onClick={this.focusTextInput}

/>

</div>

);

}

}

React will call the ref callback with the DOM element when the component mounts, and call it with null when it unmounts. Refs are guaranteed to be up-to-date before componentDidMount or componentDidUpdate fires.

You can pass callback refs between components like you can with object refs that were created with React.createRef().

function CustomTextInput(props) {

return (

<div>

<input ref={props.inputRef} />

</div>

);

}

class Parent extends React.Component {

render() {

return (

<CustomTextInput

inputRef={el => this.inputElement = el}

/>

);

}

}

In the example above, Parent passes its ref callback as an inputRef prop to the CustomTextInput, and the CustomTextInput passes the same function as a special ref attribute to the <input>. As a result, this.inputElement in Parent will be set to the DOM node corresponding to the <input> element in the CustomTextInput.

Exposing DOM Refs to Parent Components
ReactJS Keys

Get industry recognized certification – Contact us

keyboard_arrow_up
Open chat
Need help?
Hello 👋
Can we help you?