Register to get access to free programming courses with interactive exercises

Refs JS: React

React, by its very nature, totally insulates you from working directly with the DOM. But often, when it comes to integrating third-party components not written in React, we have to start thinking about direct access to the DOM. You also need a similar mechanism for text selection, focus, and media playback.

React allows you to do this with refs. It's important to understand that this mechanism is not usually necessary, and its use should be avoided as much as possible.

Consider the task of focusing on the input field:

See the Pen js_react_refs_focus by Hexlet (@hexlet) on CodePen.

ref is a component attribute whose value should be the object created in the constructor through the React.createRef() function. This object, unlike the rest of the data, which is in props or state, is stored as a normal object property. The name of the property can be anything you like. The current property of this object gives access to a DOM element, which is what you can use in componentDidMount or componentDidUpdate.

this.<property name>.current stores the DOM element of the component that ref was set for. In the example above, it's input: <input ref={this.textInput} />. The DOM element gets there (inside current) after the current component has been embedded in the real DOM, which means it can only be used in the above mentioned componentDidUpdate and componentDidMount.

Below is an example of creating a wrapper component over the popular jQuery plugin Chosen.

See the Pen js_react_refs_dom by Hexlet (@hexlet) on CodePen.

Refs can also be used on self-written components implemented as classes.

Real world use

React is convenient and easy to work with as long as we stay within React itself, but most of the existing JS libraries interact with the DOM directly, which effectively negates the benefits of React when using them. For example:

// https://github.com/kylefox/jquery-modal
$('#login-form').modal();

Including such libraries in a project will inevitably lead to the active use of lifecycle techniques and make the code more complex. This is why it's common to create so-called wrappers that hide all interaction with the DOM inside and expose the standard React interface, namely the props. One of these tasks is to resize the container. One solution is the react-resizable component. Take a look at how this component works:

const Resizable = require('react-resizable').Resizable; // or,
const ResizableBox = require('react-resizable').ResizableBox;

// ES6
import { ResizableBox } from 'react-resizable';

// ...
render() {
  return (
    <ResizableBox width={200} height={200} minConstraints={[100, 100]} maxConstraints={[300, 300]}>
      <span>Contents</span>
    </ResizableBox>
  );
}

Nothing in this code is reminiscent of the real DOM. What it comes down to is that the component is wrapped in ResizableBox, which hides all the work inside itself. The same principle is used for hundreds and maybe thousands of other components that are available on GitHub. Some of them are:


Hexlet Experts

Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

About Hexlet learning process

For full access to the course you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.bookmate">Bookmate</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.healthsamurai">Healthsamurai</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.dualboot">Dualboot</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.abbyy">Abbyy</span>
Suggested learning programs

From a novice to a developer. Get a job or your money back!

Frontend Developer icon
Profession
beginner
Development of front-end components for web applications
start anytime 10 months

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions
Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.