You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
I believe this is unexpected behaviour, however maybe it's intentional and just a bit confusing.
Given a simple use of useFetch:
functionMyComponent(){const{ loading, data }=useFetch('/some/data/source');console.log({ loading, data });if(loading)return<>Loading</>if(!data)return<>Nothing found!</>return<>Here's your {data}</>}
The render cycle with a successful data response would result in the following log and three renders:
{loading: true,data: null}// To be expected{loading: false,data: null}// Not loading anymore, but there's no data!{loading: false,data: {your: 'data'}}// The data is now populated
This middle render cycle with loading false and data (or error) still set to the previous value, or null for first render, makes it difficult to have a component trust the state of the fetch request. It also results in a flash of unwanted content, where your component renders incorrectly.
We caught this when we observed:
Our Sentry logs were reporting that our users were seeing 404 pages
In reality, they were just seeing a flash of a 404 page during first load. That render cycle was enough to trigger our error logging and was visible if you watched closely.
To Reproduce
I forked your demo and made the detail page simple:
Observe the log lines - you will get a line with No data found with the request!
You can view the sandbox here, go to pages/details/details.page.tsx
Expected behavior
There should not be an interim state where loading is false but the result isn't set on data or error.
Additional context
If this is intended behaviour, a note on how to set up a page to avoid a flash of an unloaded state would be great. I want to avoid adding a block like if(result.loading || !result.data) return <Loading />, as the absence of data on a successful request could be a valid response.
The text was updated successfully, but these errors were encountered:
stabback
changed the title
Unexpected order of updates on first data fetch
Unexpected order of updates on data fech
Nov 28, 2024
stabback
changed the title
Unexpected order of updates on data fech
Unexpected order of updates on data fetch
Nov 28, 2024
Describe the bug
I believe this is unexpected behaviour, however maybe it's intentional and just a bit confusing.
Given a simple use of
useFetch
:The render cycle with a successful data response would result in the following log and three renders:
This middle render cycle with
loading
false anddata
(orerror
) still set to the previous value, ornull
for first render, makes it difficult to have a component trust the state of the fetch request. It also results in a flash of unwanted content, where your component renders incorrectly.We caught this when we observed:
To Reproduce
I forked your demo and made the detail page simple:
No data found with the request!
You can view the sandbox here, go to
pages/details/details.page.tsx
Expected behavior
There should not be an interim state where
loading
is false but the result isn't set ondata
orerror
.Additional context
If this is intended behaviour, a note on how to set up a page to avoid a flash of an unloaded state would be great. I want to avoid adding a block like
if(result.loading || !result.data) return <Loading />
, as the absence of data on a successful request could be a valid response.The text was updated successfully, but these errors were encountered: