Refs: Manipulate the DOM in React.js
Refs provide a way to access, select or manipulate a DOM node within a React component without passing props or re-rendering the component.
Traditionally, this process can be accomplished using an ID. If you are referencing an HTML element once, this is solution works fine. But what would happen if you wanted to render a child component multiple times with each components attributes contained to itself? This is where IDs prove troublesome.
To preface, I won’t be going into detail differentiating IDs from refs. However, if you’d like an explicit (and brief) example illustrating the difference, here’s an excellent use case:
Controlled vs. Uncontrolled Components
Controlled components ensure that the parent manages and provides the prop and state values by notifying changes through callbacks like the event handler. In example below, the input value is readily accessible throughout the component without needing the value to be explicitly extracted from the input element.
class Controlled extends React.Component {
state = { input: '' }handleChange = (event) => {
this.setState({input: event.target.value})
}render() {
return (
<div>
<div>{this.state.input}</div>
<input
type='text'
value={this.state.input}
onChange={this.handleChange}
/>
</div>
)
}
}
Uncontrolled components harness the power of refs to query the DOM for node values directly by storing their own state locally. This means two things. First, uncontrolled components function similarly to traditional HTML. Second, refs manipulate the DOM directly.
class Uncontrolled extends React.Component {
state = {
input: ''
}
onSubmit = event => {
event.preventDefault()
this.setState({ input: this.input.value })
}render() {
return (
<div>
<div>{this.state.input}</div>
<form onSubmit={this.handleSubmit}>
<input type='text' ref={el=> this.input = el}/>
<button>click here</button>
</form>
</div>
)
}
}
I have found understanding the fundamental differences in uncontrolled versus controlled components has become helpful when deciding whether a particular use case demands a localized state and directly modify the DOM to accomplish my goal, or if it is possible to lean on the child’s parent.
Why Refs?
Whether an instance of a React component or a DOM element, refs provide a way to modify a child component outside of a standard parent-to-child prop pass. Meaning, in situations where you want to apply specific modifications to an element without triggering a component re-render, refs have your back.
Get Violet Moon’s stories in your inbox
Join Medium for free to get updates from this writer.
The official React docs provide three example scenarios in which you could potentially want implement a ref into your React project:
- Managing focus, text selection, or media playback.
- Triggering imperative animations.
- Integrating with third-party DOM libraries.
In the examples below, I am going to detail how to incorporate refs when wanting to focus, select text or playback media — using one of three methods for creating refs: 1) String Refs, 2) Callback Refs, and 3) React.createRef.
Example 1: String + Media Playback
Although this method will most likely be deprecated in a future React release, string refs are the oldest way of writing refs, and could potentially be helpful for understanding a traditional ref use cases.
Example 2: Callback + Text Selection
Instead of passing a string, a callback ref asks you to add an inline callback function that accepts a component-wide accessible DOM node or instance as an argument.
Another super helpful tidbit…
When using inline callback refs like we did in our example, it is important to know that for every update to the component, the callback function is called twice. First with
null, then again with the DOM element.However, creating the callback function as a bound method of the component class can be used to avoid this behavior. — Glad Chinda
Example 3: Create + Focus
React.createRef the newest (and personally-considered simplest method) for creating refs. After setting your React.createRef(), you attach your ref attribute to the DOM node you’d like to manipulate. Referring to the ref value is as simple as calling this.myCreatedRef.current.value whenever you need render the updated value.









