Updating UI Based on Async Results
Veeg om het menu te tonen
What You Are Building
Now you connect async state to the UI.
Triggering the Fetch
Dispatch the async action when the component loads:
import { useEffect } from 'react';
import { useDispatch } from 'react-redux';
import { fetchPosts } from './postsThunk';
function Posts() {
const dispatch = useDispatch();
useEffect(() => {
dispatch(fetchPosts());
}, [dispatch]);
}
Keeping UI in Sync
Once the action is dispatched:
- Redux updates the state;
- Components re-render automatically;
- UI reflects the latest data.
The Result
Your UI now responds to real data:
- Shows loading;
- Displays results;
- Handles errors.
Was alles duidelijk?
Bedankt voor je feedback!
Sectie 5. Hoofdstuk 4
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Sectie 5. Hoofdstuk 4