In React, conditional components are small, reusable components that render different UI based on props or logic. Instead of putting many if or ternary expressions directly inside a big component, you extract those conditions into focused child components.
This approach improves readability, maintainability, and separation of concerns, making your UI logic easier to understand and test.
✨ Idea: Turn conditions into components!
LoggedIn, LoggedOut).Protected decide whether to render children.Alert change style based on props.A typical pattern for conditional components is:
Conceptually:
// Pattern: parent chooses which conditional component to render
function StateA() {
return <p>State A UI</p>;
}
function StateB() {
return <p>State B UI</p>;
}
function Parent({ condition }) {
return condition ? <StateA /> : <StateB />;
}
Here, we create two small components for different user states and let the parent component decide which one to render:
// Renders different messages depending on whether the user is logged in
function LoggedIn() {
return <h4 className="text-success">Welcome back, user! ?</h4>;
}
function LoggedOut() {
return <h4 className="text-danger">Please log in to continue.</h4>;
}
function LoginControl({ isLoggedIn }) {
return isLoggedIn ? <LoggedIn /> : <LoggedOut />;
}
If you call <LoginControl isLoggedIn={true} />, React will render the <LoggedIn /> component and show a green welcome message.
If you pass isLoggedIn={false}, React will render <LoggedOut /> and show a red “Please log in” message instead.
The parent LoginControl is responsible only for choosing which component to display, making the logic easy to read and maintain.
Conditional components are ideal for handling role-based or permission-based rendering:
// Shows a different dashboard depending on the user's role
function AdminPanel() {
return <div className="border p-3">Admin Dashboard ?️</div>;
}
function UserPanel() {
return <div className="border p-3">User Dashboard ?</div>;
}
function Dashboard({ role }) {
switch (role) {
case "admin":
return <AdminPanel />;
case "user":
return <UserPanel />;
default:
return <p>Access Denied ❌</p>;
}
}
For <Dashboard role="admin" />, the admin dashboard appears. For <Dashboard role="user" />, the user dashboard appears. Any other value shows Access Denied ❌.
Adding a new role is as simple as creating a new component (for example, ManagerPanel) and extending the switch statement.
You can also create wrapper components that only render their children when a condition is met, such as authentication:
// Wrapper that protects its children behind an authentication check
function Protected({ isAuthenticated, children }) {
if (!isAuthenticated) return <p className="text-danger">Login required!</p>;
return children;
}
function App() {
const isLoggedIn = true;
return (
<Protected isAuthenticated={isLoggedIn}>
<h4>Welcome to your dashboard! ?</h4>
</Protected>
);
}
If isAuthenticated is false, the user sees Login required!. If it is true, the wrapped dashboard content is rendered.
This pattern is extremely useful for protecting routes, hiding content, or conditionally wrapping entire sections of the UI based on a central rule.
Components can decide styling or structure based on props as well:
// Reusable alert component that chooses a CSS class based on the type
function Alert({ type, message }) {
const alertClass =
type === "success"
? "alert alert-success"
: type === "error"
? "alert alert-danger"
: "alert alert-warning";
return <div className={alertClass}>{message}</div>;
}
// Usage:
<Alert type="success" message="Data saved successfully!" />
Passing different type values (like "success", "error", "warning") changes the CSS class and therefore the visual style of the alert, while the core component remains the same.
This makes your components highly reusable with dynamic behavior controlled entirely by props.
if or ternary blocks cluttering logic.GuestView or EmptyState are self-explanatory.NoData, ErrorScreen components).GuestView, AdminPanel, or EmptyState.return null in components that sometimes should render nothing.GuestView and MemberView components, and render them conditionally in a parent component.Protected component that shows its children only if a user is logged in.Alert component that changes color based on the alert type (success, error, info).if statements into multiple smaller conditional components.RoleSwitcher that renders different layout components based on a role prop.Goal: Learn how to simplify complex UI logic by breaking it into conditional components that render different outputs cleanly and maintainably.