Search⌘ K
AI Features

Using Refs for Uncontrolled Inputs

Explore the use of refs for managing uncontrolled inputs in React forms. Understand how to read and manipulate DOM-based input values on form submission without syncing every change to React state. This lesson helps you build simple, quick forms using useRef for better performance and ease of integration with third-party widgets.

Controlled components keep the UI and state perfectly in sync, but they can feel verbose for quick, low-logic forms or when integrating with third-party widgets that manage their own internal state. In such cases, letting the DOM hold the value and reading it on demand via useRef can be simpler and sometimes more performant. Remember, useRef allows us to create a reference to a DOM element or a mutable value that persists across renders without reinitializing. The trade-off is that we don’t get real-time React state updates as the user types; instead, we explicitly read values when needed (for example, on form submission).

Understanding the uncontrolled inputs

An uncontrolled input is a standard <input> (or <textarea>, <select>) whose current value lives in the DOM, not in React state. With useRef, we attach a ref to the element, and later access ref.current.value to read the input’s value at that moment.

Key ideas

...