Important Update
The Guide Feature will be discontinued after December 15th, 2023. Until then, you can continue to access and refer to the existing guides.
Author avatar

Gaurav Singhal

How to Set Focus on a React-Bootstrap Input Field Using refs.x.getDOMNode.focus

Gaurav Singhal

  • Jul 8, 2020
  • 5 Min read
  • Jul 8, 2020
  • 5 Min read
Web Development
Front End Web Development
Client-side Framework


React Bootstrap is a popular library for integrating React-ready Bootstrap user interface components. Forms are a standard part of any application, and various form controls such as text inputs, dropdowns, file uploads, radio buttons, and checkboxes are required to behave dynamically. But sometimes users submit forms with errors, such as missing required information or an invalid value. Setting focus on an input control highlights a specific input box that has an error.

In this guide, you will learn how to set focus on a React Bootstrap input control using the ref and getDOMNode() functions.

Set Focus Using refs.x.getDOMNode.focus

The React Bootstrap input control can be created inside the <Form> control, which is a part of the React Bootstrap library.

The next step is to import the required form controls from react-bootstrap, and from react-dom as given below.

1import ReactDOM from 'react-dom';
2import { Form, Button } from "react-bootstrap";

Now, set up the form with the input control and a button.

1render() {
2    return (
3      <div>
4        <span>Simple bootstrap input focus</span>
5        <div>
6          <Form>
7            <Form.Group controlId="myFormGroup">
8              <Form.Label>Email id :</Form.Label>
9              <Form.Control
10                type="email"
11                placeholder="Enter your email"
12              />
13            </Form.Group>
14            <Button
15              variant="primary"
16              type="button"
17            >
18              FOCUS
19            </Button>
20          </Form>
21        </div>
22      </div>
23    );

This is a basic form without a ref or button click event. The next move is to update the form control by adding the ref as shown below.

1<Form.Group controlId="formBasicEmail">
2    <Form.Label>Email address</Form.Label>
3    <Form.Control
4        ref={c => (this.myInputRef = c)}
5        type="email"
6        placeholder="Enter email"
7    />

There is one added property called ref with the form control used to get the reference of the input control.

After adding ref, the next thing is to add the button click event as shown below.

2    variant="primary"
3    onClick={this.setInputFocus}
4    type="button"
5    >
6    FOCUS

The button click event is one of the most important parts of this demo because once the user clicks on the button, the input control is focused.

1setInputFocus() {
2    ReactDOM.findDOMNode(this.myInputRef).focus();

Here are a few important you need to understand.

ReactDOM: The ReactDOM is a collection of methods used to modify the behavior of the various DOM elements into the components.

findDOMNode: The findDOMNode helps to get access to the specific DOM node from the DOM tree, and it can also be used to access the values coming from that DOM node.

focus(): The focus method is mostly used along with the text element. It allows you to highlight the underlying HTML element.

Below is the complete code to focus on an input element.

1import React, { Component } from "react";
2import ReactDOM from 'react-dom';
3import { Form, Button } from "react-bootstrap";
5class BootstrapInput extends Component {
6  constructor() {
7    super();
8    this.setInputFocus = this.setInputFocus.bind(this);
9  }
11  setInputFocus() {
12    ReactDOM.findDOMNode(this.myInputRef).focus();
13  }
15  render() {
16    return (
17      <div>
18        <span>Simple bootstrap input focus</span>
19        <hr />
20        <div>
21          <Form>
22            <Form.Group controlId="formBasicEmail">
23              <Form.Label>Email address</Form.Label>
24              <Form.Control
25                ref={c => (this.myInputRef = c)}
26                type="email"
27                placeholder="Enter email"
28              />
29            </Form.Group>
30            <Button
31              variant="primary"
32              onClick={this.setInputFocus}
33              type="button"
34            >
35              FOCUS
36            </Button>
37          </Form>
38        </div>
39      </div>
40    );
41  }
44export default BootstrapInput;

Focus Input Without Using findDOMNode

You have learned how to focus on a React Bootstrap input element using findDOMNode, but alternatively, it can also be achieved by just using the ref.

There is one simple change while accessing the DOM element as shown below.

1setInputFocus() {
2    this.myInputRef.focus();

The focus() method directly uses ref and the input element can be focused.

Note: findDOMNode() is deprecated in strict mode, and from now on only ref is sufficient to access the DOM nodes directly.


React supports DOM manipulation methods and property by using the package react-dom, which allows us to access and manipulate the DOM element's behavior.

In this guide, you have seen two different approaches to focus the input element: one using findDOMNode() along with focus(), and one using just the element ref. I hope this guide helps you access the DOM elements.