Uncontrolled Components in React
Learn how uncontrolled form elements manage their own values and how React can access those values using refs.
1. Introduction to Uncontrolled Components
An uncontrolled component is a form element that manages its own value through the DOM instead of having its value controlled by React state.
React can access the current value of an uncontrolled input when needed. A common approach is to use the useRef hook to create a reference to the input element.
value and
onChange. Uncontrolled inputs commonly use
defaultValue or refs to access their values.
2. How Uncontrolled Components Work
In an uncontrolled input, the browser maintains the current input value. React can read that value through a ref when an action such as a button click or form submission occurs.
3. Using defaultValue
The defaultValue prop sets the initial value of an uncontrolled text input. After the input is displayed, the user can change its value without React controlling every change through state.
Example
function StudentInput() {
return (
<div>
<label htmlFor="studentName">
Student Name:
</label>
<input
id="studentName"
type="text"
defaultValue="Rahul"
/>
</div>
);
}
export default StudentInput;
Expected Output
The input initially displays "Rahul". The defaultValue prop sets the initial value rather than controlling every subsequent change.
4. Accessing Input Values Using useRef
The useRef hook can hold a reference to a DOM input element. We can read the current input value through the ref when a button is clicked.
Example
import { useRef } from "react";
function NameForm() {
const nameRef = useRef(null);
function handleClick() {
alert(nameRef.current.value);
}
return (
<div>
<label htmlFor="name">Name:</label>
<input
id="name"
type="text"
ref={nameRef}
defaultValue="Rahul"
/>
<button onClick={handleClick}>
Show Name
</button>
</div>
);
}
export default NameForm;
Expected Result
Clicking the button in the running React application displays the current input value in an alert.
The ref's current property refers to the input element. Its value property contains the text entered by the user.
5. Handling Form Submission with Refs
We can use refs to read uncontrolled input values when a form is submitted. The submit handler can then process the values without storing every keystroke in React state.
Example
import { useRef } from "react";
function LoginForm() {
const usernameRef = useRef(null);
const passwordRef = useRef(null);
function handleSubmit(event) {
event.preventDefault();
const username = usernameRef.current.value;
const password = passwordRef.current.value;
console.log("Username:", username);
console.log("Password entered:", password.length > 0);
}
return (
<form onSubmit={handleSubmit}>
<label htmlFor="username">
Username:
</label>
<input
id="username"
ref={usernameRef}
defaultValue=""
/>
<label htmlFor="password">
Password:
</label>
<input
id="password"
type="password"
ref={passwordRef}
defaultValue=""
/>
<button type="submit">
Submit
</button>
</form>
);
}
export default LoginForm;
Expected Console Output
Username: Rahul
Password entered: true
The handler reads both values when the form is submitted. This example logs only whether a password was entered, rather than printing the password itself.
6. Using Refs with Textarea
A textarea is used to collect multiline text. We can attach a ref to it and read the entered text when required.
Example
import { useRef } from "react";
function FeedbackForm() {
const feedbackRef = useRef(null);
function handleSubmit(event) {
event.preventDefault();
console.log(feedbackRef.current.value);
}
return (
<form onSubmit={handleSubmit}>
<label htmlFor="feedback">
Feedback:
</label>
<textarea
id="feedback"
ref={feedbackRef}
defaultValue=""
/>
<button type="submit">
Submit Feedback
</button>
</form>
);
}
export default FeedbackForm;
Expected Result
When submitted, the entered feedback is written to the console.
The ref provides access to the textarea element, and its current value contains the entered feedback.
7. Controlled vs Uncontrolled Components
Both approaches can collect user input, but they differ in how the current value is managed and accessed.
| Feature | Controlled | Uncontrolled |
|---|---|---|
| Value management | React state | DOM element |
| Common props | value and onChange | defaultValue and ref |
| Reading values | From state | From the input element |
| React updates | State updates on input changes | Value can change without state updates |
| Typical use | Live validation and dynamic UI | Reading values when needed |
8. Common Mistakes
Mistake 1: Forgetting to Attach the Ref
const nameRef = useRef(null);
function handleClick() {
console.log(nameRef.current.value);
}
return (
<input defaultValue="Rahul" />
);
The input does not have the ref prop, so nameRef.current does not refer to that input element.
Correct Code
<input
ref={nameRef}
defaultValue="Rahul"
/>
Mistake 2: Accessing the Value Before Mounting
const nameRef = useRef(null);
console.log(nameRef.current.value);
Initially, nameRef.current is null before React attaches the ref to the input element. Accessing value at that time causes an error.
Correct Code
function handleClick() {
if (nameRef.current) {
console.log(nameRef.current.value);
}
}
Mistake 3: Confusing defaultValue and value
<input
defaultValue={name}
onChange={handleChange}
/>
defaultValue sets the initial value of an uncontrolled input. It does not make the input a controlled component.
Correct Controlled Input
<input
value={name}
onChange={handleChange}
/>
9. Practice Tasks
Complete these tasks to practise uncontrolled components.
Summary
Uncontrolled components allow form elements to maintain their values in the DOM instead of controlling every value through React state. The defaultValue prop can provide an initial input value, and the useRef hook can access the current value when needed. Refs can be used with text inputs, password fields, and textareas. Controlled components are often useful for live validation and state-driven interfaces, while uncontrolled components can be useful when values only need to be read at specific moments, such as during form submission.