React Portals — Complete Guide
React Portals — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of React.js Tutorial on Toolliyo Academy.
On this page
React.js Tutorial · Lesson 35 of 100
Controlled Forms
Beginner ✓ → Intermediate → Advanced → Professional
Intermediate · 2 — Building apps · ~14 min read · Module 4: Routing & Forms
Introduction
You know the basics now. Here we use Controlled Forms in real app situations — forms, pages, and data. Still plain language, just a bit more depth. A controlled input gets its value from React state and updates through onChange. React is the single source of truth for the field value. You can validate on every keystroke, disable submit until valid, and reset the form by resetting state.
Routing is how a single React app feels like many pages. Users expect URLs like /cart and /checkout — Router makes that work.
When will you use this?
Add routing when your app has more than one screen and URLs should be bookmarkable.
- Multi-page apps like dashboards use React Router — /settings, /profile, /orders.
- E-commerce sites route /product/123 to a product detail component.
Real-world: Practo patient intake form
Clinic registration binds every input to React state so staff can validate phone length and show inline errors before API submit.
Production-style code
function PatientForm({ onSave }) {
const [form, setForm] = useState({ name: '', phone: '', age: '' });
function update(field) {
return e => setForm(prev => ({ ...prev, [field]: e.target.value }));
}
function handleSubmit(e) {
e.preventDefault();
if (form.phone.length !== 10) return alert('Enter 10-digit mobile');
onSave(form);
}
return (
<form onSubmit={handleSubmit}>
<input value={form.name} onChange={update('name')} placeholder="Full name" />
<input value={form.phone} onChange={update('phone')} placeholder="Mobile" />
<input value={form.age} onChange={update('age')} placeholder="Age" />
<button type="submit">Register</button>
</form>
);
}
What happens in production: Controlled inputs are the default for forms that need validation, masking, or disable-submit until valid — every fintech KYC flow uses this.
Lesson example (start here)
Copy this smaller example first. Once it works, compare it with the real-world code above.
function SignUp() {
const [email, setEmail] = useState('');
return (
<form onSubmit={e => { e.preventDefault(); console.log(email); }}>
<input
type="email"
value={email}
onChange={e => setEmail(e.target.value)}
/>
<button type="submit">Sign up</button>
</form>
);
}
Line-by-line walkthrough
| Code | What it means |
|---|---|
function SignUp() { | Defines a function — often a component or event handler. |
const [email, setEmail] = useState(''); | Creates state: a value that can change, plus a function to update it. |
return ( | Sends UI or a value back to whoever called this function. |
<form onSubmit={e => { e.preventDefault(); console.log(email); }}> | Defines a function — often a component or event handler. |
<input | JSX tag — a UI element or custom component on the page. |
type="email" | Part of the Controlled Forms example — read it together with the lines before and after. |
value={email} | Part of the Controlled Forms example — read it together with the lines before and after. |
onChange={e => setEmail(e.target.value)} | Defines a function — often a component or event handler. |
/> | Part of the Controlled Forms example — read it together with the lines before and after. |
<button type="submit">Sign up</button> | JSX tag — a UI element or custom component on the page. |
</form> | Part of the Controlled Forms example — read it together with the lines before and after. |
); | Closes a block started by { or ( above. |
} | Closes a block started by { or ( above. |
How it works (big picture)
- value={email} ties the input to state.
- onChange reads e.target.value and calls setEmail.
- The input cannot change without React updating state.
Do this on your computer
- Add useState for each field.
- Set value and onChange on inputs.
- Handle submit with preventDefault and read state.
- Read the real-world section and name which part of the app uses this topic.
- Run the example locally and confirm the same behavior in the browser.
- Change one value in the example (text, initial state, or URL) and predict what will happen before you save.
Experiments — try changing this
- Change text or labels in the example and save — watch the browser update.
- Break the code on purpose (remove a bracket), read the error message, then fix it.
- Change the initial state value and see the starting UI change.
- Open React DevTools (browser extension) while running Controlled Forms and inspect component props/state.
Remember
Controlled = value + onChange + state. Form data lives in React state. Standard pattern for small forms.
Common questions
Controlled vs uncontrolled?
Controlled: React owns the value. Uncontrolled: DOM holds it; you read with ref on submit.
How long should I spend on Controlled Forms?
Until you can explain it in your own words and run the example without looking at the answer. Beginners often need 30–60 minutes per new hook or routing topic; setup lessons may take one afternoon.
What if I get stuck on Controlled Forms?
Re-read the line-by-line walkthrough, check the browser console for red errors, and compare your code character-by-character with the example. Search the exact error text — someone else had it too.
Where is Controlled Forms used in real jobs?
See the real-world section above — the same pattern appears in LMS, banking, e-commerce, and SaaS products. Interviewers ask you to explain it using one concrete example from your project or this lesson.