Controlled Forms — Complete Guide
Controlled Forms — 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 18 of 100
Conditional Rendering
Beginner → Intermediate → Advanced → Professional
Beginner · 1 — Foundations · ~12 min read · Module 2: JavaScript & React Fundamentals
Introduction
This lesson is part of the beginner section. We explain Conditional Rendering slowly, with examples you can copy and run. If something is unclear, read it twice — that is how everyone learns. Conditional rendering means showing different UI based on a condition — logged in vs logged out, loading vs loaded, error vs success. Real apps always branch: hide the dashboard until login, show a spinner while fetching, show an error message on failure.
Conditional Rendering appears in almost every file you will write. Once it clicks, later lessons (hooks, routing, APIs) become much easier.
When will you use this?
You use this in every component you write from your first app to your tenth year on the job.
- Product pages, login forms, and todo lists all use components, props, and state.
- When a user types in a search box, React state holds the text and filters a list.
Real-world: Toolliyo login gate
Toolliyo shows marketing homepage to guests and course dashboard to logged-in students. One App shell switches entire layout based on auth state.
Production-style code
function AppShell({ user, children }) {
if (user === undefined) {
return <FullPageSpinner label="Checking session…" />;
}
if (!user) {
return <LoginPage />;
}
return (
<Layout user={user}>
<StudentDashboard user={user} />
{children}
</Layout>
);
}
What happens in production: Clear auth branches prevent showing grades or payment pages to anonymous visitors — a legal and security requirement for LMS products.
Lesson example (start here)
Copy this smaller example first. Once it works, compare it with the real-world code above.
function Greeting({ isLoggedIn, name }) {
if (!isLoggedIn) {
return <p>Please log in.</p>;
}
return <p>Welcome, {name}!</p>;
}
Line-by-line walkthrough
| Code | What it means |
|---|---|
function Greeting({ isLoggedIn, name }) { | Defines a function — often a component or event handler. |
if (!isLoggedIn) { | Part of the Conditional Rendering example — read it together with the lines before and after. |
return <p>Please log in.</p>; | Sends UI or a value back to whoever called this function. |
} | Closes a block started by { or ( above. |
return <p>Welcome, {name}!</p>; | Sends UI or a value back to whoever called this function. |
} | Closes a block started by { or ( above. |
How it works (big picture)
- Early return is the clearest pattern for two big branches.
- For small inline cases you can use condition &&
or a ternary.
Do this on your computer
- Add a boolean state isOpen and toggle a panel.
- Show a loading message when data is null.
- Try: {error &&
{error}
} - 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.
Remember
Use if/return, &&, or ternary to show/hide UI. Keep conditions readable — extract sub-components if needed.
Common questions
&& vs ternary?
&& for show/hide one thing; ternary for either/or two things.
How long should I spend on Conditional Rendering?
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 Conditional Rendering?
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 Conditional Rendering 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.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!