1. Controlled vs Uncontrolled Inputs

Every form input in React falls into one of two categories: controlled or uncontrolled. The difference is about who owns the value — React state or the DOM.

🎯

Controlled (recommended)

React state drives the input's value. You set value={state} and update it via onChange. React is the single source of truth.

🌐

Uncontrolled

The DOM owns the value. You read it when you need it using a ref. Feels like vanilla HTML but gives you less control.

Rule of thumb: Always prefer controlled inputs. They make validation, conditional disabling, and form submission predictable. Use uncontrolled only when integrating with third-party libraries or for one-off file inputs.
controlled-input.jsx React Owns the Value
Controlled Input Data Flow
User Types
keystroke
→
onChange Fires
e.target.value
→
setState
updates state
→
Re-render
input shows new value

2. onChange + State

The onChange handler fires on every keystroke. It receives a synthetic event, and e.target.value gives you the current input value. You store it in state, and React re-renders with the new value.

two-way-binding.jsx State Drives the Input
🛒
UrbanCart Example
Real-time search filtering

On UrbanCart's product page, typing into the search bar instantly filters the product list. This works because the search query lives in state. Every keystroke triggers onChange, updates state, and React re-renders the filtered list — no page reload needed.

Why not just read the DOM? In vanilla JS you might do document.getElementById('search').value. In React, that bypasses the component lifecycle. If state doesn't know about the value, conditional rendering, validation, and form submission all break.

3. Form Submission

Wrap your inputs in a <form> tag and handle the onSubmit event. Always call e.preventDefault() to stop the browser from reloading the page.

form-submit.jsx Handling Form Submit
Why e.preventDefault()? Without it, the browser sends a traditional HTTP request and reloads the page. In a React SPA, you handle everything in JavaScript — you never want a full page reload on form submit.

4. Textarea & Select

In React, <textarea> and <select> follow the same controlled pattern as <input>. Set value to state and update via onChange.

Textarea

textarea.jsx Controlled Textarea

Select (Dropdown)

select.jsx UrbanCart Payment Method
HTML vs React: In HTML, a <select> uses the selected attribute on an <option>. In React, you set value on the <select> itself — much cleaner.

5. Checkbox

Checkboxes use checked instead of value, and e.target.checked instead of e.target.value. The state is a boolean.

checkbox.jsx Terms & Conditions
Key difference: For text inputs you read e.target.value. For checkboxes you read e.target.checked (a boolean). Mixing these up is one of the most common React form bugs.

6. Validation

Client-side validation checks fields before submission. Store error messages in state and display them conditionally. You can also disable the submit button until all fields are valid.

validation.jsx Validate on Submit
🛡️
Pro Tip
Validation is a UX feature, not a security feature

Client-side validation improves user experience — it gives instant feedback. But never rely on it for security. Always validate again on the server. A malicious user can bypass your React code entirely by hitting the API directly.

7. useRef for Uncontrolled Inputs

Sometimes you need to read an input's value without tracking every keystroke. The useRef hook gives you a direct reference to the DOM node.

useref-input.jsx Uncontrolled with useRef
✅

When to use useRef

File inputs, integrating with non-React code, focus management, or when you truly don't need re-renders on input change.

⚠️

When NOT to use useRef

Any form that needs validation, conditional rendering, or real-time display of input values. Use controlled inputs instead.

🛒 Try It — Checkout Form

Build an UrbanCart checkout form with name, email, address, payment method (select), and terms agreement (checkbox). Validate that all fields are filled and terms are accepted before showing a success message.

🛠️ Try It Yourself — UrbanCart Checkout Form
Output

⭐ Try It — Product Review Form

Build a product review form: pick a rating (1–5 select), add a title, write a comment (textarea), and submit. Display the submitted review below the form.

🛠️ Try It Yourself — Product Review
Output

Test your knowledge

Question 1 of 5
What makes an input "controlled" in React?
Question 2 of 5
Why must you call e.preventDefault() in a React form's onSubmit handler?
Question 3 of 5
How do you read a checkbox's current state in its onChange handler?
Question 4 of 5
In React, how do you set the selected value of a <select> dropdown?
Question 5 of 5
When should you use useRef for form inputs instead of controlled components?
0
/5
Great job!

Up Next

Lesson 20: React Router →
Next up: give UrbanCart multiple pages with client-side routing.
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

You just built real checkout forms.
Now build a career that ships products.

Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses →