π React Conditional Rendering β The Complete Guide for Beginners
π React Conditional Rendering

React apps interact with users β and based on the userβs action or the appβs state, we often need to show or hide UI elements.
This is where Conditional Rendering comes in.
It allows you to render components based on conditions, just like if-else statements in JavaScript.
In this blog, youβll learn:
β What is conditional rendering?
β Different methods of conditional rendering
β if/else
β Ternary operator
β Logical AND (&&)
β Inline rendering
β Switch-case patterns
β Showing/hiding components
β Real-world best practices
Letβs dive in π
π What Is Conditional Rendering in React?
Conditional rendering means:
Render different UI elements depending on the applicationβs state.
Simple example:
{isLoggedIn ? <Dashboard /> : <Login />}
π© 1. Using if/else Statements
This is the most straightforward approach.
function Message({ isLoggedIn }) {
if (isLoggedIn) {
return <h2>Welcome Back!</h2>;
} else {
return <h2>Please Login</h2>;
}
}
β Clean
β Easy to understand
β Not suitable for inline JSX
πͺ 2. Using the Ternary Operator
Most commonly used because it's compact.
const isOnline = true;
return (
<div>
{isOnline ? <p>User is Online</p> : <p>User is Offline</p>}
</div>
);
β Short & clean
β Works inside JSX
β Best for simple conditions
π§ 3. Using Logical AND (&&)
Used when you want to render something only if the condition is true.
const hasNotification = true;
return (
<div>
<h1>Dashboard</h1>
{hasNotification && <p>You have new notifications</p>}
</div>
);
β Perfect for optional UI
β Shortest syntax
β Doesnβt work well with 0 (because 0 is falsy)
π¨ 4. Using Logical OR (||) for Fallback UI
Render fallback content if the first value is falsy.
const username = "";
return <h2>{username || "Guest User"}</h2>;
β Useful for default values
β Clean syntax
π¦ 5. Conditional Rendering with Functions
Move complex logic outside JSX.
function getStatus(isActive) {
if (isActive) {
return <p>Status: Active</p>;
}
return <p>Status: Inactive</p>;
}
export default function App() {
return <div>{getStatus(true)}</div>;
}
β Cleaner JSX
β Useful for large components
π« 6. Using Switch-Case Pattern
React doesn't support switch inside JSX, but you can use it inside functions.
function RenderComponent({ type }) {
switch (type) {
case "admin":
return <h2>Welcome Admin</h2>;
case "user":
return <h2>Hello User</h2>;
default:
return <h2>Guest Access</h2>;
}
}
β Great for multiple conditions
β Avoids nested ternaries
π₯ 7. Inline Conditional Rendering (One-Liners)
return (
<div>
{count > 5 && <p>Count is greater than 5</p>}
</div>
);
β Simple
β Fast
β Not recommended for complicated logic
π¬ 8. Hiding Components Instead of Removing Them
Sometimes, you want to hide a component, not remove it.
<div style={{ display: isOpen ? "block" : "none" }}>
<p>This is hidden or shown</p>
</div>
β Keeps component in DOM
β Useful for dropdowns & modals
π― Real-World Use Cases
β Loading State
{loading ? <p>Loading...</p> : <Data />}
β Authentication
{user ? <Dashboard /> : <Login />}
β Role-Based UI
{role === "admin" && <AdminPanel />}
β Form Validation
{error && <p className="error">{error}</p>}
β Best Practices for Conditional Rendering
β Avoid deeply nested ternaries
β Use functions for complex logic
β Prefer ternary operator for simple conditions
β Use && for optional elements
β Keep JSX clean and readable
β Extract UI into components when conditions grow
π Conclusion
Conditional rendering is a core feature of React that makes your app dynamic and interactive.
In this guide, you learned:
β All React conditional rendering techniques
β if/else, ternary, &&, ||
β Component hiding
β Switch-case rendering
β Real examples and best practices



