Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Updating UI Based on Async Results | Handling Async Logic with Redux Toolkit
Gestion d'État avec Redux Toolkit dans React

bookUpdating UI Based on Async Results

Glissez pour afficher le menu

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.
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 5. Chapitre 4

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Section 5. Chapitre 4
some-alt