Different Ways to Create Refs in React

Introduction 

 
While writing React apps we are always following the component-based architecture but there might be a situation where you may have to manipulate or imperatively modify the DOM element. So to accomplished, this React provides something called Refs. In simple terms, refs give access to the underlying DOM element.
 
As per the React documentation below are the best use cases of using refs:
  • Managing focus, text selection, or media playback.
  • Triggering imperative animations.
  • Integrating with third-party DOM libraries. 
Note
Avoid using refs everywhere into the app. If things can be done using a declarative manner don't use refs.
 
In this article, we are going to see the different ways of creating refs in react along with some examples. So let's grab a cup of coffee and start coding.

TL;DR

 
So today we will be discussing three ways of creating refs in React:
  • Callback Refs
  • Using React.createRef() API (from React 16.3)
  • Using useRef() hook 

Callback Refs

 
As the name implies, in callback refs, we have to provide a callback function to ref props. So the function receives input as a DOM element which we can store in a variable for later use in the application. For example, when a user clicks on the button the input element should have focus. First, I am creating a component along with input and a button.
  1. import React, { Component } from "react";  
  2.   
  3. class CallbackRefExample extends Component {  
  4.   constructor(props) {  
  5.     super(props);  
  6.   }  
  7.   
  8.   render() {  
  9.     return (  
  10.       <div>  
  11.         Callback Ref Example:  
  12.         <br />  
  13.         <br />  
  14.         <input type="text" />  
  15.         <button style={{ margin: "8px" }}>Click</button>  
  16.       </div>  
  17.     );  
  18.   }  
  19. }  
  20.   
  21. export default CallbackRefExample;  
I am dividing this into three steps. First, create a callback function and bind this in the constructor. I have created a callback function called inputElementRef.
  1. import React, { Component } from "react";  
  2.   
  3. class CallbackRefExample extends Component {  
  4.   constructor(props) {  
  5.     super(props);  
  6.     this.inputElementRef = this.inputElementRef.bind(this);  
  7.   }  
  8.   
  9.   inputElementRef(inputElement) {  
  10.     this.inputRef = inputElement;  
  11.   }  
  12.   
  13.   render() {  
  14.     return (  
  15.       <div>  
  16.         Callback Ref Example:  
  17.         <br />  
  18.         <br />  
  19.         <input type="text" />  
  20.         <button style={{ margin: "8px" }}>Click</button>  
  21.       </div>  
  22.     );  
  23.   }  
  24. }  
  25. export default CallbackRefExample;  
Second, assess the inputElementRef() function to ref props of an input element
  1. <input type="text" ref={this.inputElementRef}/>  
We have now created our callback ref.
 
Third, create on the click handler function to call the focus method using inputRef.
  1. handleClick(){  
  2.   this.inputRef.focus();  
  3. }  
Assign this function to the onClick event of the button.
  1. <button style={{ margin: "8px" }} onClick={this.handleClick} >Click</button>  
The final output should look like:
 
 Different Ways To Create Refs In React

Using React.createRef() API

 
In 16.3 version of React has introduced a new API called React.createRef() for creating refs. We don't need to create a callback function and assign it to ref props here. So just create a ref and stored it into some variable and assign this variable to the ref prop of the DOM element. So we will take the same example, create a functional component that has one input element and one button.
  1. import React, { Component } from "react";  
  2.   
  3. export default function CallbackRefExample() {  
  4.   return (  
  5.     <div>  
  6.       Callback Ref Example:  
  7.       <br />  
  8.       <br />  
  9.       <input type="text" />  
  10.       <button style={{ margin: "8px" }}>Click</button>  
  11.     </div>  
  12.   );  
  13. }  
Now create a variable called inputRef and assign it with React.createRef() API.
  1. let inputRef = React.createRef();  
Now apply this inputRef to ref props of input element. After that, create onClick handler for a button so that when button onClick event fires, we have to focus the input element.
 
So the final component looks like:
  1. import React, { Component } from "react";  
  2.   
  3. export default function CallbackRefExample() {  
  4.   let inputRef = React.createRef();  
  5.   
  6.   const handleClick = () => {  
  7.     inputRef.current.focus();  
  8.   };  
  9.   
  10.   return (  
  11.     <div>  
  12.       Callback Ref Example:  
  13.       <br />  
  14.       <br />  
  15.       <input type="text" ref={inputRef} />  
  16.       <button style={{ margin: "8px" }} onClick={handleClick}>  
  17.         Click  
  18.       </button>  
  19.     </div>  
  20.   );  
  21. }  
Here, we will receive the mounted instance of the component in its current property of ref. That's why we have invoked the focus() function.
  1. inputRef.current.focus();  
Output
 
Different Ways To Create Refs In React 

Using the useRef() hook

 
We can now create a ref using hooks. React provides us inbuilt hook called useRef(). useRef hook is taking an initial value for the ref as input. Similar to React.createRef(), we have to create a variable that can store the ref reference for further use. Here is an example:
 
Output
 
Different Ways To Create Refs In React

Conclusion

 
In this article, I explained different ways to create Refs in React JS. I also created simple examples using ref. I really hope that you enjoyed this article, please do not hesitate to send me your thoughts or comments on what I could have done better. This article was originally published on my blog.
 
You can follow me on twitter @sumitkharche01
 
Happy Coding!