React / 31
Loading states, shimmer UI & conditional rendering
Show the right screen for loading, error, empty and success, using early returns, ternaries and &&, with a shimmer that matches your layout.
Four states, one component
| State | What happened | Show |
|---|---|---|
| loading | Request in flight | A shimmer in the shape of the content |
| error | Network or server failed | A clear message and a "Try again" button |
| empty | It worked, but there's nothing | "No recipes found", maybe a hint |
| success | Data is here | The real UI |
Loading, error, empty, data
Try itstatus = "idle"
if (status === "loading") return <Shimmer />if (status === "error") return <ErrorBox />if (recipes.length === 0) return <Empty />return <RecipeGrid recipes={recipes} />Nothing loaded yet. Press a button.
Conditional rendering: three tools
Since a component is just a function, choosing what to show is plain JavaScript.
1 · Early return for whole-screen states:
if (status === "loading") return <Shimmer />;
if (status === "error") return <p role="alert">Couldn't load recipes.</p>;
if (recipes.length === 0) return <p>No recipes found.</p>;
return (
<div className="grid">
{recipes.map((recipe) => (
<RecipeCard key={recipe.id} recipe={recipe} />
))}
</div>
);2 · Ternary (condition ? a : b) to pick one of two things inside JSX:
<button onClick={() => setLoggedIn(!loggedIn)}>
{loggedIn ? "Log out" : "Log in"}
</button>3 · && when the other option is "nothing":
{error && <p role="alert">{error.message}</p>}if can't go inside JSX's { } because it's a statement, not a value. Ternaries and && are expressions, so they can.
Shimmer UI (skeletons)
A shimmer, or skeleton, is a set of grey placeholder blocks in the same shape as the content, often with a soft moving highlight. Compared with a spinner or "Loading…":
- The user sees the layout straight away, so the wait feels shorter.
- When data lands, cards replace placeholders of the same size, so nothing jumps.
export default function Shimmer({ count = 8 }) {
return (
<div className="grid" role="status" aria-label="Loading recipes">
{Array.from({ length: count }, (_, i) => (
<div key={i} className="shimmer-card" />
))}
</div>
);
}.shimmer-card {
height: 240px;
border-radius: 12px;
background: linear-gradient(90deg, #e5e5e5 25%, #f2f2f2 50%, #e5e5e5 75%);
background-size: 200% 100%;
animation: shimmer 1.4s linear infinite;
}
@keyframes shimmer {
from { background-position: 200% 0; }
to { background-position: -200% 0; }
}
@media (prefers-reduced-motion: reduce) {
.shimmer-card { animation: none; }
}Array.from({ length: 8 }, …) makes eight placeholders without hand-writing them. The role="status" label tells screen-reader users what's going on, since the grey boxes say nothing on their own.
Don't
Use the list's length as the loading flag
return recipes.length === 0 ? <Shimmer /> : <RecipeGrid />;A search with no matches, or an API that returns [], also has length 0, so you shimmer forever.
Do
Track loading separately
if (status === "loading") return <Shimmer />;
if (recipes.length === 0) return <p>No recipes found.</p>;
return <RecipeGrid />;Loading and empty are different states, so they get different screens.
Gotchas
Recall
Can you answer these?
0/5
Answer out loud (or in your head) before you tap. That's what makes it stick.
Saved on this device only. No login.
