Navigate back to the homepage
Browse Repo

All about React's createRef method

Felix Gerschau
February 1st, 2021 · 4 min read

React refs are a useful and powerful tool that we can use to get even more control over the elements in our application.

In this article, you’ll learn what you need to know about refs and how to use them in your class and function components.

Do you want to see a working example? Click here to skip the explanation.

Or have a look at the codepen here.

What are refs in React?

“Ref” is short for reference, and it’s a React feature that allows us to store a component or DOM element of our application in a variable and access it programmatically.

We can use refs to access DOM elements and React components.

When (not) to use refs

The official React documentation states that the following use cases are a good fit for using refs:

  • Managing focus, text selection, or media playback.
  • Triggering imperative animations.
  • Integrating with third-party DOM libraries.

They also say that we should avoid using refs for anything that can be done declaratively.

Declaratively in React means we should use the JSX syntax whenever we can, instead of using JavaScript to update our application.

Leaving the DOM updates to React makes sense. After all, React is designed to update the elements on our websites as efficiently as possible.

Sometimes though, React doesn’t provide the functionality we need, in which case we need to fall back to plain JavaScript.

If we were to get a DOM element in Vanilla JavaScript, we’d use functions like document.getElementById or document.getElementsByClassName.

1const inputElement = document.getElementById('input-element-id');

But React offers a more convenient way to access elements in our application: Refs.

Every class component and HTML element in React accepts the ref prop, which we can use to store a reference to this element.

How to use createRef

That was enough of theory for now. Let’s have a look at how we can use refs in our applications.

For this tutorial, I created a small codepen that allows you to enter your name and focus the input when you click on the button.

React createRef example

The HTML input element doesn’t accept any focus prop that we could use to focus it. However, the HTML node does expose the focus function.

We somehow need to get access to the element in our code so we can call this function. Instead using getElementById, we’ll use React’s refs.

First, we need to create a new variable in our class where we will store the reference. Mine has the creative name inputRef.

1constructor(props) {
2 super(props);
3 this.inputRef = React.createRef();
4 // ...

We initialize the variable with createRef, which allows us to simply pass the variable to the element that we want to reference.

2 ref={this.inputRef}
3 value={}
4 onChange={this.handleInputChange}

That’s everything we need to do to get access to the input element.

The inputRef variable now contains a reference to the input element.

If we were to inspect the variable at this point, it would look something like this.

1▶ Object
2 ▶ current: input
3 ▶ __proto__: Object

We can focus the input element by calling the focus function in the onClick handler function.

2 onClick={() => this.inputRef.current?.focus()}
4 Focus

In case you are wondering, adding the question mark is called the optional chaining, first introduced by TypeScript. It allows us to safely access current without worrying about it being undefined or null.

Full example

Putting everything together, the code of that component looks like the following:

1class App extends React.Component {
2 constructor(props) {
3 super(props);
4 this.inputRef = React.createRef();
5 this.state = {
6 name: '',
7 };
8 }
10 handleInputChange = (e) => {
11 e.preventDefault();
12 this.setState({ name: });
13 }
15 render() {
16 return (
17 <>
18 <h1>Hi { || 'there'}! ??</h1>
19 <p>
20 What's your name?
21 </p>
22 <input
23 ref={this.inputRef}
24 value={}
25 onChange={this.handleInputChange}
26 />
27 <button
28 onClick={() => this.inputRef?.current?.focus()}
29 >
30 Focus
31 </button>
32 </>
33 );
34 }

This code is also available on codepen.

Referencing class components

With refs, we can also reference other React components. When we do this, instead of receiving a DOM node, current will contain the mounted instance of that component.

Note that this doesn’t work with function components because, unlike class components, they don’t have instances.

When dealing with class components, the syntax is the same as with DOM elements:

1<MyComponent ref={this.myComponentRef} />

You don’t need to change anything in MyComponent.

Referencing function components using forwardRef

I know that just one moment ago I said that you can’t reference function components. However, if you really need to do this, you can forward the ref to a DOM element.

For that, we use the forwardRef function.

1const MyInputComponent = React.forwardRef((props, ref) => (
2 <input ref={ref} />

This doesn’t allow us to reference the functional component itself but one of the native elements that it renders.

With this modification, we can use the ref prop as we did with class components and HTML elements.

1const MyInputComponent = React.forwardRef((props, ref) => {
2 return (
3 <input
4 ref={ref}
5 value={}
6 onChange={props.onChange}
7 />
8 );

In App we can pass down the ref property like before.

1class App extends React.Component {
2 //...
4 render() {
5 return (
6 <>
7 <MyInputComponent
8 ref={this.inputRef}
9 name={}
10 onChange={this.handleChange}
11 />
13 </>
14 );
15 }

I created a seperate codepen for this example.

Other ways to reference elements

Above I only explained one way of creating refs because I believe it’s the easiest way of doing so.

You can do this differently though, and you might encounter these ways of creating refs in some code bases.

Callback refs

The ref prop, besides accepting objects created with createRef, also accepts a callback.

2 ref={(e) => this.inputRef = e}
3 value={}
4 onChange={this.handleInputChange}

Note that we don’t access the current field in the callback because the function directly receives the element.

Because of this, we don’t need to initialze the variable with createRef. We can set it to null in the constructor:

1constructor(props) {
2 super(props);
3 this.inputRef = null;
4 this.state = {
5 name: '',
6 };

When accessing the element in our code, we can again omit current as inputRef already contains the element:

2 onClick={() => this.inputRef?.focus()}
4 Focus

The callback will be called with the element once the component mounts, and it will be called with null before the component unmounts.

React guarantees that it will be up to date before componentDidMount or componentDidUpdate are executed.

How to use useRef

The createRef function we know from class components also has its counterpart for functional components: The useRef hook.

We first need to initialize the variable.

1const inputRef = useRef(null);

We then pass this value to the ref prop:

2 ref={inputRef}
3 value={name}
4 onChange={handleChange}

The usage of this hook is very similar to createRef. However, there are some other use-cases that I discuss in my article about React’s useRef hook.

Observability for Production React Apps

While you’re testing the createRef method, and how to interact directly with DOM references, debugging your React apps in production may be challenging and time consuming. OpenReplay is an open-source session replay stack for developers. It helps you replay everything your users do and shows how your app behaves and renders for every issue. It’s like having your browser’s inspector open while looking over your user’s shoulder. OpenReplay Frontend Monitoring OpenReplay helps to quickly get to the root cause by reproducing issues as if they happened in your own browser. It also monitors your frontend performance by capturing key metrics such as page load time, memory consumption and slow network requests as well as Redux actions/state. Happy debugging, for modern frontend teams - Start monitoring your web app for free.


React’s refs are a useful tool for situations where we can’t do things the ”React way“.

I hope that with this article, you have a good overview of how and when to use them.

More articles from OpenReplay Blog

Fetch vs Axios: Which Is the Best Library for Making HTTP Requests?

Axios and Fetch provide a seemingly equal API and set of features, so which one is better for making HTTP requests?

January 29th, 2021 · 4 min read

React Snapshot Testing With Jest: An Introduction With Examples

Both frontend and backend developers write tests. Testing is an excellent way to guarantee code quality.

January 29th, 2021 · 5 min read
© 2021 OpenReplay Blog
Link to $ to $ to $