Web apps contain various HTML elements rendered into the DOM, and based on user interaction, any of the specific DOM elements can be accessed through code. Accessing DOM elements allows you to fetch elements' HTML content, their child elements, or sometimes a specific element's value to process it.
There are various ways to access any of the DOM elements. In this guide, you are going to learn a few approaches that can help you access some specific DOM elements.
ref
is a common approach to access various DOM elements that work on setting the ref
to an element and accessing it based on the name of ref
in the component.
Based on the official docs, ref
is suitable for:
With React 16.3.0, the way to create ref
is different now than in earlier versions of React. Below is the basic syntax to create the ref
.
1this.ref_name = React.createRef();
The ref
created using createRef()
and the variable ref_name
can be attached to any DOM element.
To demonstrate, create a simple form with input control and a button. Once a user clicks on the button, the input value should be accessed through the ref
.
1constructor(props) {
2 super(props);
3 // Created ref
4 this.myInputRef = React.createRef();
5 this.getInputValue = this.getInputValue.bind(this);
6}
The ref
created gets the constructor's input value, creates the form, and assigns ref
to the input element.
1render() {
2 return (
3 <>
4 <div>
5 <input type="text" ref={this.myInputRef} />
6 <button onClick={this.getInputValue}>Submit</button>
7 </div>
8 </>
9 );
10}
Along with the <input>
control, an additional prop is used, ref
, followed by the name of the ref created by you previously. Below is the function from which the value of the input gets accessed.
1getInputValue() {
2 const inputValue = this.myInputRef.current.value;
3 console.log(inputValue)
4}
So using the statement this.myInputRef.current.value
, the input value can be accessed directly. If you want to access the complete HTML element, you can do it as demonstrated below.
1const inputElement = this.myInputRef.current;
One thing to keep in mind is that you cannot use ref
with a function because it does not have any instance to the current component.
So far in this guide, you have seen how ref
is used to access a DOM element and its value. However, there are other approaches that can also be helpful, and one of them is ReactDOM.findDOMNode()
.
findDOMNode()
is used to find a specific node from the DOM tree and access its various properties, such as its inner HTML, child elements, type of element, etc. In short, it can return all the supported DOM measurements related to the kind of element.
The syntax to use findDOMNode()
in React is as follows:
1ReactDOM.findDOMNode(component_name)
Along with findDOMNode()
, you can pass the name of the component or element you want to access from the DOM. To illustrate the concept, let's create an example that has input control and a button. With a click on the button, input control should be focused.
Create the ref
for input control is as demonstrated below.
1constructor(props) {
2 super(props);
3 this.myInputRef = React.createRef();
4 this.state = {};
5 this.onFocusInput = this.onFocusInput.bind(this);
6}
After creating the ref
, create one input control and add additional props called ref
. Once the user clicks the button, the input will be accessed from findDOMNode()
.
1render() {
2 return (
3 <>
4 <div>
5 <input type="text" ref={this.myInputRef} />
6 <button onClick={this.onFocusInput}>FOCUS</button>
7 </div>
8 </>
9 );
10}
Once the user clicks on the focus button, it goes to the function onFocusInput
and tries to find the DOM element based on the ref
and then uses the focus()
method to focus the selected input control.
1onFocusInput() {
2 ReactDOM.findDOMNode(this.myInputRef.current).focus();
3}
Observe that as soon as you click on the button, input control is auto-focused, which is done by using findDOMNode()
along with the focus()
method.
Note : As per the official docs,
findDOMNode()
only works on mounted components (that is, components that are placed in the DOM.) If you try to call on an element that has yet not mounted (like callingfindDOMNode()
inrender()
on a component that has yet to be created), an exception will get thrown.findDOMNode
cannot be used on function components.
Accessing DOM elements and their current values is a top concern when working in web apps. There should be a suitable approach that makes your DOM manipulation way easier than ever before.
All DOM elements in React can be accessed in multiple ways, such as ref
or findDOMNode()
, so choose your implementation approach wisely. I hope this guide helped you understand how to access DOM elements.