Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Updating UI Based on Async Results | Handling Async Logic with Redux Toolkit
State Management med Redux Toolkit i React

bookUpdating UI Based on Async Results

Stryg for at vise menuen

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.
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 5. Kapitel 4

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Sektion 5. Kapitel 4
some-alt