Using defaultValue for Text Inputs
What you'll learn: How to set an initial value for an uncontrolled text input without converting it into a controlled component.
When working with uncontrolled components, you often want to provide a starting value—perhaps pre-filling a form with saved data or setting a default like "Enter your name here." The defaultValue prop lets you do exactly this.
The Core Difference
Remember, value makes an input controlled—React owns the value and requires onChange to update it. With defaultValue, you're saying: "Here's the initial value, but after that, the DOM handles it."
Think of it like giving someone directions to start a journey versus controlling their every turn. defaultValue points them in the right direction, then lets them drive freely.
How It Works
function SignupForm() {
const nameRef = useRef(null);
function handleSubmit(e) {
e.preventDefault();
console.log(nameRef.current.value); // Read the final value
}
return (
<form onSubmit={handleSubmit}>
<input
ref={nameRef}
defaultValue="Jane Doe"
type="text"
/>
<button type="submit">Submit</button>
</form>
);
}
The input starts with "Jane Doe," but the user can freely change it. You're not tracking every keystroke with onChange—you just read the final value via the ref when needed.
Works with Textareas Too
<textarea
ref={commentRef}
defaultValue="Initial comment text..."
/>
Same pattern applies to <textarea> elements.
Why Use It?
defaultValue is perfect when you need a starting value but don't need live validation or conditional rendering based on the input. It keeps your component simpler with no state management overhead.
Key Takeaway: Use defaultValue to set initial values for uncontrolled inputs—the DOM takes over after the first render, and you read the final value via refs when needed.