Skip to article
detaID ↗
← Back home

Conditional Rendering: Keep It Inside the Component

Best practice for placing conditional rendering logic within React components using context or internal state.

2 min read
#React#Best Practices#Clean Code

When building React applications, a recurring question is:

Should I conditionally render a component from the parent or within the component itself?

The answer lies in React's core principle: place logic where the state lives.

Real-World Example: Notification Banner

Let’s say you want to show a notification when there's a new message. You use context to track notification state.

✅ Correct Approach: Let the Child Handle It

jsx
// NotificationBanner.js import React, { useContext } from "react"; import { NotificationContext } from "./NotificationContext"; const NotificationBanner = () => { const { hasNewNotification, message } = useContext(NotificationContext); if (!hasNewNotification) return null; return ( <div className="rounded bg-yellow-100 p-4 shadow"> <strong>New Notification:</strong> {message} </div> ); }; export default NotificationBanner;
jsx
// Dashboard.js import NotificationBanner from "./NotificationBanner"; const Dashboard = () => ( <div> <NotificationBanner /> {/* Other dashboard content */} </div> );

Why this works well:

  • The component owns the logic of when to show/hide itself.
  • The parent stays clean and agnostic to the inner behavior.
  • Easier to reuse and reason about the component.

❌ Less Ideal: Parent Handles the Logic

jsx
// Dashboard.js import NotificationBanner from "./NotificationBanner"; import { useContext } from "react"; import { NotificationContext } from "./NotificationContext"; const Dashboard = () => { const { hasNewNotification, message } = useContext(NotificationContext); return ( <div> {hasNewNotification && <NotificationBanner message={message} />} {/* Other dashboard content */} </div> ); };

Downside: the parent has to understand logic that belongs to the child, reducing modularity and reusability.


🔚 Conclusion

  • Keep conditional rendering inside the component if it depends on internal or context-driven state.
  • This makes your React components more maintainable, modular, and aligned with idiomatic best practices.

More writing

A few more notes from my archive.

Using a Combination of Data Attributes for React Keys

In React, assigning a unique `key` prop to elements in a list is crucial for performance and UI consistency. When natural unique identifiers like `id` are unavailable, developers often default to using array indices, which can lead to UI inconsistencies. A better approach is to generate a unique key by combining multiple data attributes. This article explores how to effectively use data attributes as keys in React lists.

Testing Priorities in the AI Era: Smoke Tests and Documentation First

My take on modern testing priorities: start with smoke tests and documentation, then layer in e2e, component, and unit tests. A practical approach for AI-powered development.

Fixing the 'Ghost Changes' Bug in Bulk Edit Tables

Deep dive into debugging a tricky state management issue where the system detected changes that didn't actually exist, and how comparing with original values solved the problem.

When You're Stuck Waiting for Help, Try Cleaning Up Your Code — You Never Know…

Refactoring your code when you're stuck might just be the key to finding the bug and unblocking yourself.

Leave a comment