Chuck's Academy

Intermediate React

Refs and Direct DOM Manipulation

In React, direct manipulation of the DOM is less common due to the virtual DOM representation system. However, in certain cases, it is necessary to interact directly with DOM elements. In this chapter, we will learn how to use Refs to access and manipulate DOM elements and explore some practical cases.

Dom manipulation with RefsDom manipulation with Refs

What Are Refs in React?

Refs are a function provided by React to reference elements or components created in the DOM. They allow us to directly manipulate the DOM of these elements without compromising React's declarative structure, being useful for cases like animations, form control, and managing external elements.

Creating and Basic Use of a Ref

Refs are created using the useRef hook in functional components. The created reference is maintained throughout the component's entire lifecycle without being reinitialized at every render.

Basic Usage Example with useRef

In this example, we use a Ref to automatically focus an input field when the component mounts:

javascript
"In this example, we create a reference to an input using useRef and focus it automatically when the component mounts, using inputRef.current.focus within useEffect."

DOM Elements Manipulation with Refs

Refs are useful for directly controlling DOM elements when necessary, such as playing or pausing a video or manipulating style properties of a specific element.

Example: Controlling a Video Element

Here, we use a Ref to pause or play a video through control buttons:

javascript
"In this example, we use videoRef to access the video element and control it using the play and pause methods, triggered from buttons outside the video."

Refs and Controlled Components

Although Refs are useful for directly accessing the DOM, in forms it is preferable to use controlled components with states. However, in situations where direct access is necessary, Refs can facilitate the process.

Example: Form Validation with Refs

This example shows how to use Refs to access multiple form fields and validate them upon submission:

javascript
"In this example, we use the references nameRef and emailRef to access the form field values and check if they are empty upon submission."

Advanced Use of Refs: Forwarding Refs

Forwarding Refs allows passing a ref to a child component, granting direct access to a DOM element within the child component. This is useful when a reference is needed in reusable components or libraries.

Forwarding Refs Example

Here we have a CustomInput component that accepts a Ref and forwards it to the internal input:

javascript
"In this example, we use forwardRef so that CustomInput accepts an external ref and passes it to the internal input, allowing the parent component to control the input's focus."

Using the Component with Forwarding Refs

To use the CustomInput component, a Ref is assigned from the parent component:

javascript
"Here, we pass a reference inputRef to the CustomInput component, allowing the focus button to activate the input internally."

Best Practices When Using Refs

  • Use Refs Sparingly: Refs are a powerful tool, but excessive use is not recommended, as they can complicate the application's state management.
  • Prefer Controlled Components for Forms: In forms, controlled components with states are more declarative and easier to maintain.
  • Forwarding Refs for Reusable Components: Implement forwardRef in reusable components to facilitate DOM access from parent components.

Conclusion

Refs and direct DOM manipulation are advanced tools that allow accessing and manipulating specific elements in a React application. In this chapter, we explored the basic use of Refs, DOM elements manipulation, and forwarding Refs for reusable components.


Ask me anything