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.
keystroke
e.target.value
updates state
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.
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.
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.
4. Textarea & Select
In React, <textarea> and <select> follow the same controlled pattern as <input>. Set value to state and update via onChange.
Textarea
Select (Dropdown)
<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.
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.
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.
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 — 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.
Test your knowledge
e.preventDefault() in a React form's onSubmit handler?<select> dropdown?useRef for form inputs instead of controlled components?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 →