Managing Loading and Error States
Sveip for å vise menyen
The Problem
When working with async data, the UI must react to different states.
Handling States
You can use the status and error values from the store:
- Show loading indicators;
- Show data when available;
- Show errors when something fails.
const { items, status, error } = useSelector((state) => state.posts);
if (status === 'loading') {
return <p>Loading...</p>;
}
if (status === 'failed') {
return <p>Error: {error}</p>;
}
return (
<ul>
{items.map((post) => (
<li key={post.id}>{post.title}</li>
))}
</ul>
);
Users need feedback. Without handling these states, the app feels broken or unresponsive.
Alt var klart?
Takk for tilbakemeldingene dine!
Seksjon 5. Kapittel 3
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Seksjon 5. Kapittel 3