Answer the question
In order to leave comments, you need to log in
Redux - API request completion rate?
Good afternoon. Already asked this question on another resource, but no one answered there. I'll try my luck here.
The question concerns the implementation of the execution status of an asynchronous request, thanks to which the component will understand when the data has loaded and can be used when rendering (saga / thunk - it doesn’t matter).
For example, I fetch a list of some items from the server, then in the parent component I create a list of cards for each item using .map(). I need the component to understand when the data has arrived and it can start using it for rendering.
All the tutorials say that the reducer needs the loading, isLoading property (and so on):
const INITIAL_STATE = {
allGamesCollection: null,
loading: false,
error: null
}
case AllGamesActionTypes.FETCH_ALLGAMES_START:
return {
...state,
loading: true
}
case AllGamesActionTypes.FETCH_ALLGAMES_SUCCESS:
return {
...state,
allGamesCollection: action.payload,
loading: false,
error: null
}
case AllGamesActionTypes.FETCH_ALLGAMES_FAILURE:
return {
...state,
error: action.payload,
loading: false,
allGamesCollection: null
}
{
loading ? <Spinner /> : allGames.map(game => <GamePreviewItem game={game} key={game.id} />)
}
Answer the question
In order to leave comments, you need to log in
What if it's like this?
const INITIAL_STATE = {
allGamesCollection: [],
loading: false,
error: null
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question