Accessing the Input Value
What you'll learn: How to read the current value from an uncontrolled input using a ref, without needing state.
In the previous lessons, you learned how to create a ref with useRef and attach it to an input element. Now comes the payoff: actually reading the value the user has typed.
The Core Idea
Instead of maintaining the input's value in state (like controlled components do), you access it on-demand via the ref. The key property you need is:
inputRef.current.value
This gives you the live DOM value at the moment you access it—typically when a form is submitted or when you need to do something with the input.
How It Works
When you attach a ref to an input with ref={inputRef}, React stores a reference to the actual DOM element in inputRef.current. That DOM element has a .value property that always reflects what's currently in the input field.
Think of it like this: Instead of asking React to tell you what's in the box (state), you're reaching directly into the box (DOM) and checking yourself.
Common Pattern: Form Submission
function MyForm() {
const nameRef = useRef(null);
function handleSubmit(event) {
event.preventDefault();
const enteredName = nameRef.current.value;
console.log('User entered:', enteredName);
}
return (
<form onSubmit={handleSubmit}>
<input type="text" ref={nameRef} />
<button type="submit">Submit</button>
</form>
);
}
Notice: no onChange, no state, no re-renders while typing. You only read the value when you actually need it.
Key Takeaway: With uncontrolled components, access the current input value via inputRef.current.value whenever you need it—no state updates required.