Handling Empty Lists in React
Learn how to handle empty arrays, display fallback messages, and render list data conditionally in React.
1. Introduction to Empty Lists
In React, lists are commonly displayed using the map() method. Sometimes an array contains no items. For example, a student list may be empty because no students have been added yet.
When an array is empty, the map() method does not render any list items. We can check the array length and display a helpful message instead of leaving the page blank.
array.length === 0 to check whether a list is empty.
2. Rendering an Empty Array
When we use map() with an empty array, React has no list items to render. The surrounding JSX can still appear on the page.
Example
function StudentList() {
const students = [];
return (
<div>
<h2>Student List</h2>
<ul>
{students.map((student) => (
<li key={student.id}>
{student.name}
</li>
))}
</ul>
</div>
);
}
export default StudentList;
Expected Output
Student List
No list items are displayed because the students array is empty.
The heading is displayed, but the map() method returns no list items because the array contains no students.
3. Checking Whether a List Is Empty
The length property tells us how many elements an array contains. If the length is 0, the array is empty.
Example
function CourseList() {
const courses = [];
return (
<div>
<h2>Available Courses</h2>
{courses.length === 0 && (
<p>No courses available.</p>
)}
</div>
);
}
export default CourseList;
Expected Output
Available Courses
No courses available.
courses.length === 0 is true
because the array contains no elements. Therefore, React
displays the message.
4. How Empty List Handling Works
The following diagram explains how React checks an array before deciding what content to display.
5. Handling Empty Lists Using If-Else
We can use an if-else statement to decide whether to display an empty message or a list of items. This approach is useful when the two situations need different JSX.
Example
function BookList() {
const books = [];
if (books.length === 0) {
return <p>No books found.</p>;
}
return (
<ul>
{books.map((book) => (
<li key={book.id}>
{book.title}
</li>
))}
</ul>
);
}
export default BookList;
Expected Output
No books found.
Since the books array is empty, the component returns the message. The list is rendered only when books are available.
6. Handling Empty Lists Using the Ternary Operator
The ternary operator provides a short way to choose between two outputs. We can use it to display either an empty message or the list of available items.
Example
function TaskList() {
const tasks = [];
return (
<div>
<h2>My Tasks</h2>
{tasks.length === 0 ? (
<p>No tasks available.</p>
) : (
<ul>
{tasks.map((task) => (
<li key={task.id}>
{task.name}
</li>
))}
</ul>
)}
</div>
);
}
export default TaskList;
Expected Output
My Tasks
No tasks available.
7. Displaying Empty and Non-Empty Lists
The same component can handle both empty and non-empty arrays. When data is available, React displays the items. Otherwise, React displays a message.
Example: Product List
function ProductList() {
const products = [
{ id: 1, name: "Keyboard" },
{ id: 2, name: "Mouse" }
];
return (
<div>
<h2>Products</h2>
{products.length === 0 ? (
<p>No products available.</p>
) : (
<ul>
{products.map((product) => (
<li key={product.id}>
{product.name}
</li>
))}
</ul>
)}
</div>
);
}
export default ProductList;
Expected Output
Products
- Keyboard
- Mouse
The products array contains two objects, so the condition is false and React renders both products using map().
8. Common Mistakes
Mistake 1: Checking the Array Incorrectly
const courses = [];
if (courses.length) {
console.log("No courses available.");
}
An empty array has a length of 0, which is falsy in a conditional expression. Therefore, this condition does not display the message for an empty array.
Correct Code
const courses = [];
if (courses.length === 0) {
console.log("No courses available.");
}
Mistake 2: Using an Incorrect Empty Condition
const students = [];
{students.length > 0 && (
<p>No students available.</p>
)}
The condition is true only when students are available. Therefore, this code does not show the empty message when the array is empty.
Correct Code
const students = [];
{students.length === 0 && (
<p>No students available.</p>
)}
length === 0 for empty arrays and
length > 0 for non-empty arrays.
9. Practice Tasks
Complete these tasks to practise handling empty lists in React.
Summary
Handling empty lists in React helps provide clear feedback when an array contains no elements. The length property can check whether an array is empty, and conditional rendering can display an appropriate message or render available items using map(). We can use logical AND, if-else statements, or the ternary operator to handle empty and non-empty lists. These techniques make list components easier to understand and more useful.