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
function Stock() {
const { data, isLoading, isValidating } = useSWR(STOCK_API, fetcher);
if (isLoading) return <div className="skeleton" />;
if (data?.length === 0) return <div className="empty"> no result </div>
return (
<>
<div>${data}</div>
{isValidating ? <div className="spinner" /> : null}
</>
);
}
As described in the example above, I want to display a “no result” empty state when the fetched data is empty. However, before the data fetching begins — during the initial React render — both isLoading and isValidating are false, which means I can’t return a loading state. As a result, the component mistakenly renders the empty state. How can I correctly detect the state where “the request has not yet started”?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
As described in the example above, I want to display a “no result” empty state when the fetched data is empty. However, before the data fetching begins — during the initial React render — both isLoading and isValidating are false, which means I can’t return a loading state. As a result, the component mistakenly renders the empty state. How can I correctly detect the state where “the request has not yet started”?
Beta Was this translation helpful? Give feedback.
All reactions