state={networkState:'NotAsked',// || 'Loading' || 'Failure' || 'Success',data:null,// use this data only if `networkState === 'Success'error:null,// use this data only if `networkState === 'Failure'}
What do you think of this approach? (This may be a second question 😅)
I have a question about network request.
Usually I will use a
isFetching
state for network requesting.isFetching === false
, render with empty data.componentDidMount
, dispatch fetch request, setisFetching
to true, render with a<Loading >
component.isFetching
to false, render with fetched data or error message.Is there a good(safe) way to initially render a
<Loading>
component, rather than rendering with empty data. Thanks~I think using the
isFetching
state may not reflect the "fact", it is actually consist of the four state as the post point out:And the elm code in the post:
And we may change the state to:
What do you think of this approach? (This may be a second question 😅)
This makes sense to me. There are also other states with pagination (e.g. “nothing more to fetch”).