How to Render Empty Lists and Fallback UI in React

An empty array should not render an empty page. Learn how to show fallback UI when a React list has no items, and keep the check readable.

5 min read

To render empty lists in React, check the array length before you map. When the array has no items, show fallback UI instead of an empty container. That way the screen always explains what the user is looking at, and a clear fallback turns a confusing blank area into a useful message.

Here is the check as an early return:

App.jsxApp.jsx
function TaskList({ tasks }) {
  if (tasks.length === 0) {
    return <p>No tasks yet.</p>;
  }
  return <ul>{tasks.map((task) => <li key={task.id}>{task.title}</li>)}</ul>;
}

When tasks is empty, the page shows "No tasks yet." instead of an empty list. When tasks has items, the normal list renders.

The early return keeps the fallback separate from the list code, and each mapped item still gets a stable key. This pattern is the fastest way to guarantee the user never stares at a blank screen.

Keep the fallback next to the list

A ternary expresses the same choice inside the return statement. It reads well when the fallback is a single element and the list stays short.

App.jsxApp.jsx
function TaskList({ tasks }) {
  return tasks.length === 0 ? (
    <p>No tasks yet.</p>
  ) : (
    <ul>{tasks.map((task) => <li key={task.id}>{task.title}</li>)}</ul>
  );
}

Both branches now live side by side. Reading the return statement tells the whole story in one place: a message when empty, a list otherwise.

Pick the style that matches the surrounding code, then stay consistent across the component. Either form works, so the choice is about readability, not correctness.

Why the check looks at length

An empty array is truthy in JavaScript. Writing a condition on the array itself does not catch the empty case, because an empty array still counts as true.

Only checking the length answers the real question: are there zero items? The same check handles filtered results, where zero matches is a normal outcome rather than an error. That is why every empty-state example here compares length to zero instead of testing the array.

Decide what empty means

An empty array can mean three different things, and each one deserves a different message:

  • Nothing has been added yet, so a new account shows "No tasks yet."
  • A search returned no matches, so show "No tasks match your search."
  • The data failed to load, which is an error state rather than an empty one.

When data comes from a network request, check loading and error states before the length check. Those states are separate from an empty result, and mixing them confuses the user about what actually happened. A short, specific message is worth more than a generic placeholder.

The list side is covered in the guide on rendering lists with map. For the branching itself, see the guide on conditional rendering in React.

Rune AI

Rune AI

Key Insights

  • Check the array length before you map the list.
  • Return fallback UI when the length is zero.
  • Use an early return or a ternary to keep the code tidy.
  • Distinguish empty from loading and from no search results.
  • An empty array is truthy, so test its length.
RunePowered by Rune AI

Frequently Asked Questions

Why should I show a fallback when a list is empty?

An empty container tells the user nothing. A fallback message explains whether there is no data yet, no results, or an error, which keeps the screen understandable.

Where should the empty check go?

Put it right where the list renders. Check the array length before the map call, and return the fallback first so the list code below stays simple.

Is an empty array truthy or falsy in JavaScript?

An empty array is truthy. Check its length instead, such as items.length === 0, because checking the array itself would not catch the empty case.

Conclusion

An empty list deserves a visible answer. Check the array length before mapping, return a fallback message when it is zero, and keep the real list rendering below. The same check also handles a missing result set cleanly.