Conditional rendering in React allows components to render different elements or components based on certain conditions. This is a fundamental concept that lets you build dynamic and responsive user interfaces.
Key Concepts:
function App() {
const isLoggedIn = true;
return (
<div>
{isLoggedIn ? <h1>Welcome back!</h1> : <h1>Please sign in.</h1>}
</div>
);
}
In this example, the message displayed will depend on the value of isLoggedIn
.
function App() {
const items = ['Apple', 'Banana', 'Cherry'];
return (
<ul>
{items.map((item, index) => (
<li key={index}>{item}</li>
))}
</ul>
);
}
Here, the list items are generated dynamically based on the contents of the items
array.
Conditional rendering can be done in various ways, depending on the complexity of the condition and the desired output. Understanding these techniques will help you build more flexible and maintainable components.
React allows you to include conditionals directly within your JSX. The most common patterns are:
Example:
function App() {
const user = { name: 'John' };
return (
<div>
<h1>Hello, {user ? user.name : 'Guest'}!</h1>
</div>
);
}
Example:
function App() {
const isAdmin = true;
return (
<div>
<h1>Welcome User</h1>
{isAdmin && <button>Admin Panel</button>}
</div>
);
}
Example:
function App() {
const isLoggedIn = true;
function renderMessage() {
if (isLoggedIn) {
return <h1>Welcome back!</h1>;
} else {
return <h1>Please sign in.</h1>;
}
}
return (
<div>
{renderMessage()}
</div>
);
}
Sometimes, you may want to render nothing based on a condition. In React, you can return null
to prevent anything from rendering.
Example:
function App() {
const hasError = false;
return (
<div>
{hasError ? <h1>Error occurred!</h1> : null}
</div>
);
}
For more complex conditions, using a switch
statement inside a function can be more readable and maintainable than a series of if-else
statements.
Example:
function App() {
const status = 'loading';
function renderContent() {
switch (status) {
case 'loading':
return <h1>Loading...</h1>;
case 'success':
return <h1>Data loaded successfully!</h1>;
case 'error':
return <h1>Error loading data!</h1>;
default:
return <h1>Unknown status</h1>;
}
}
return (
<div>
{renderContent()}
</div>
);
}
Rendering lists in React is common, but it’s essential to follow best practices to ensure optimal performance and avoid common pitfalls.
When rendering lists in React, each item should have a unique key
prop. Keys help React identify which items have changed, been added, or removed, improving the performance of rendering.
Key Guidelines:
Example with Proper Keys:
function App() {
const items = [
{ id: 1, name: 'Apple' },
{ id: 2, name: 'Banana' },
{ id: 3, name: 'Cherry' }
];
return (
<ul>
{items.map(item => (
<li key={item.id}>{item.name}</li>
))}
</ul>
);
}
You might want to conditionally render certain elements within a list. Combining conditional rendering with lists allows for dynamic and responsive UI elements.
Example:
function App() {
const users = [
{ id: 1, name: 'John', isAdmin: true },
{ id: 2, name: 'Jane', isAdmin: false },
{ id: 3, name: 'Doe', isAdmin: false }
];
return (
<ul>
{users.map(user => (
<li key={user.id}>
{user.name} {user.isAdmin && <span>(Admin)</span>}
</li>
))}
</ul>
);
}
When rendering lists, it’s crucial to consider the scenario where the list might be empty. Providing a fallback UI in such cases improves the user experience.
Example:
function App() {
const items = [];
return (
<div>
{items.length > 0 ? (
<ul>
{items.map(item => (
<li key={item.id}>{item.name}</li>
))}
</ul>
) : (
<p>No items available</p>
)}
</div>
);
}
When rendering large lists, performance can become an issue. Here are some strategies to handle this:
React.memo
: To avoid unnecessary re-renders of list items.react-window
or react-virtualized
to only render the visible portion of the list.Example of React.memo:
const ListItem = React.memo(function ListItem({ item }) {
return <li>{item.name}</li>;
});
function App() {
const items = [
{ id: 1, name: 'Apple' },
{ id: 2, name: 'Banana' },
{ id: 3, name: 'Cherry' }
];
return (
<ul>
{items.map(item => (
<ListItem key={item.id} item={item} />
))}
</ul>
);
}
React’s reconciliation process is how it updates the DOM to match your React elements. Keys play a crucial role in this process by helping React identify which elements have changed, been added, or removed.
Example Without Proper Keys:
function App() {
const items = ['Apple', 'Banana', 'Cherry'];
return (
<ul>
{items.map((item, index) => (
<li key={index}>{item}</li>
))}
</ul>
);
}
If items are reordered or new items are added, React may get confused about which item is which, leading to potential bugs.
To reinforce your understanding of conditional rendering and list handling in React, here are some helpful videos:
For further reading and exploration: