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.
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:
The Hexlet support team or other students will answer you.
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.
Programming courses for beginners and experienced developers. Start training for free
Our graduates work in companies:
From a novice to a developer. Get a job or your money back!
Sign up or sign in
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.