Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

React Refs

React Refs

React supports a very special attribute Ref that you can use to attach to any component output by render().

This special attribute allows you to reference the corresponding backing instance returned by render(), ensuring that you always get the correct instance at any time.

In React, refs provide a way to access DOM elements or component instances. Using refs, you can directly manipulate DOM elements or obtain child component instances, making them suitable for handling focus, text selection, media playback, triggering forced animations, or integrating with third-party DOM libraries.

Usage

Use React.createRef or useRef to create and access refs.

React.createRef is typically used in class components, while useRef is a Hook typically used in function components.

In addition, binding this in event handlers can use class field syntax to avoid creating a new function on every render.

Complete example

You can use this to get the current React component, or use a ref to get a reference to a component, as shown in the following example:

React example

class

MyComponent

extends

React

.

Component

{

constructor

(

props

)

{

super

(

props

)

;

this

.

myInputRef

=

React

.

createRef

(

)

;

}

handleClick

=

(

)

=>

{

//

Using the native DOM API to set focus

this

.

myInputRef

.

current

.

focus

(

)

;

}

render

(

)

{

return

(

<div>
<input type="text" ref={this>

.

myInputRef

}

/> <

input

type

=

"

button

"

value

=

"

Click me to focus the input

"

onClick

=

{

this

.

handleClick

}

/>

div

)

;

}

}

Try it »

In the example, we obtained a reference to the backing instance of the input, so after clicking the button the input gains focus.

We can also use the getDOMNode() method to get the DOM element


Creating Refs

React provides the React.createRef method to create refs.

Example

import

React from

'react'

;

import

ReactDOM from

'react-dom'

;

class

MyComponent

extends

React.

Component

{

  constructor

(

props

)

{

   

super

(

props

)

;

   

this

.

myRef

=

React.

createRef

(

)

;

 

}

  componentDidMount

(

)

{

   

this

.

myRef

.

current

.

focus

(

)

;

 

}

  render

(

)

{

   

return

<

input type

=

"text"

ref

=

{

this

.

myRef

}

/>;

 

}

}

const

root

=

ReactDOM.

createRoot

(

document.

getElementById

(

"root"

)

)

;

// Render the MyComponent component

root.

render

(

<

MyComponent

/>

)

;

Callback Refs

Another way to create refs is by using callback functions. This approach was common before React 16.3, but React.createRef is now more recommended.

Example

class

MyComponent

extends

React.

Component

{

  constructor

(

props

)

{

   

super

(

props

)

;

   

this

.

setMyRef

=

element

=>

{

     

this

.

myRef

=

element

;

   

}

;

 

}

  componentDidMount

(

)

{

   

if

(

this

.

myRef

)

{

     

this

.

myRef

.

focus

(

)

;

   

}

 

}

  render

(

)

{

   

return

<

input type

=

"text"

ref

=

{

this

.

setMyRef

}

/>;

 

}

}

const

root

=

ReactDOM.

createRoot

(

document.

getElementById

(

"root"

)

)

;

// Render the MyComponent component

root.

render

(

<

MyComponent

/>

)

;

Using Refs to Access DOM Elements

Refs allow direct access to and manipulation of DOM elements.

Example

class

MyComponent

extends

React.

Component

{

  constructor

(

props

)

{

   

super

(

props

)

;

   

this

.

myRef

=

React.

createRef

(

)

;

 

}

  handleClick

=

(

)

=>

{

   

this

.

myRef

.

current

.

style

.

backgroundColor

=

'yellow'

;

 

}

;

  render

(

)

{

   

return

(

     

<

div

<

input type

=

"text"

ref

=

{

this

.

myRef

}

/>

       

<

button onClick

=

{

this

.

handleClick

}

Change Background

button

     

div

   

)

;

 

}

}

const

root

=

ReactDOM.

createRoot

(

document.

getElementById

(

"root"

)

)

;

// Render the MyComponent component

root.

render

(

<

MyComponent

/>

)

;

Using Refs to Access Child Component Instances

Refs can also be used to access a child component's instance methods or properties.

Example

class

ChildComponent

extends

React.

Component

{

  focusInput

=

(

)

=>

{

   

this

.

inputRef

.

current

.

focus

(

)

;

 

}

;

  constructor

(

props

)

{

   

super

(

props

)

;

   

this

.

inputRef

=

React.

createRef

(

)

;

 

}

  render

(

)

{

   

return

<

input type

=

"text"

ref

=

{

this

.

inputRef

}

/>;

 

}

}

class

ParentComponent

extends

React.

Component

{

  constructor

(

props

)

{

   

super

(

props

)

;

   

this

.

childRef

=

React.

createRef

(

)

;

 

}

  handleClick

=

(

)

=>

{

   

this

.

childRef

.

current

.

focusInput

(

)

;

 

}

;

  render

(

)

{

   

return

(

     

<

div

<

ChildComponent ref

=

{

this

.

childRef

}

/>

       

<

button onClick

=

{

this

.

handleClick

}

Focus Child Input

button

     

div

   

)

;

 

}

}

const

root

=

ReactDOM.

createRoot

(

document.

getElementById

(

"root"

)

)

;

// Render the MyComponent component

root.

render

(

<

MyComponent

/>

)

;

Using the useRef Hook (Function Components)

In function components, you can use the useRef Hook to create refs.

Example

import

React

,

{

useRef

}

from

'react'

;

import

ReactDOM from

'react-dom'

;

const

MyComponent

=

(

)

=>

{

 

const

inputRef

=

useRef

(

null

)

;

 

const

handleClick

=

(

)

=>

{

    inputRef.

current

.

focus

(

)

;

 

}

;

 

return

(

   

<

div

<

input type

=

"text"

ref

=

{

inputRef

}

/>

     

<

button onClick

=

{

handleClick

}

Focus Input

button

   

div

 

)

;

}

;

const

root

=

ReactDOM.

createRoot

(

document.

getElementById

(

"root"

)

)

;

// Render the MyComponent component

root.

render

(

<

MyComponent

/>

)

;

Summary