Conditional Rendering
Question
How do you conditionally render a component in React?
Hint
Common patterns: {cond && <Comp/>} or {cond ? <A/> : <B/>}.
Explanation
React supports several conditional rendering patterns. The && operator: {isLoggedIn && <Dashboard />} renders Dashboard only if isLoggedIn is truthy. The ternary: {isLoggedIn ? <Dashboard /> : <Login />} renders one of two options. For early returns, use if statements before the return. Avoid putting numbers directly with && (0 && <Comp/> renders 0). Use ternary or Boolean() for numbers. Choose the pattern that is most readable for each case.
Related Resources
Related Lessons
- Conditional Rendering
Learn conditional rendering.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.