React Conditionals — Complete Guide
React Conditionals — 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 15 of 100
React Conditionals
Beginner → Intermediate → Advanced → Professional
Beginner · 1 — Learn by example · ~6 min · Module 2: Props, Events & Lists
What is this?
Conditional rendering means showing different UI based on a condition — logged in vs logged out, loading vs loaded, error vs success.
Why should you care?
Real apps always branch: hide the dashboard until login, show a spinner while fetching, show an error message on failure.
See it live — copy this example
Paste into src/App.jsx (or the file noted in the steps). Save and watch the browser update.
function Greeting({ isLoggedIn, name }) {
if (!isLoggedIn) {
return <p>Please log in.</p>;
}
return <p>Welcome, {name}!</p>;
}
Run Example »
Edit the code and click Run to see the result update live.
What happened?
- Early return is the clearest pattern for two big branches.
- For small inline cases you can use condition &&
or a ternary.
Try it yourself
- Add a boolean state isOpen and toggle a panel.
- Show a loading message when data is null.
- Try: {error &&
{error}
} - 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.