-
Notifications
You must be signed in to change notification settings - Fork 353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
v2 blog post #351
v2 blog post #351
Changes from 1 commit
334bc73
4864c51
4fd396d
7a4d955
d19c599
cb689a1
0e95a85
5a9fc79
a4799bd
241f460
a8cc95b
823e149
1cc2f14
e6092a4
e2a8c3e
f0dda25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,3 +1,4 @@ | ||||||||
import Callout from 'nextra-theme-docs/callout' | ||||||||
import Video from 'components/video' | ||||||||
|
||||||||
# Understanding SWR | ||||||||
|
@@ -42,6 +43,42 @@ This pattern is to fetch data and change the key and revalidate it later with th | |||||||
|
||||||||
 | ||||||||
|
||||||||
## Combining with isLoading and isValidating for better UX | ||||||||
|
||||||||
`isLoading` and `isValidating` are similar, so you might be confused about which value you should use. | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
`isValidating` becomes `true` whenever there is an ongoing request, on the other hand, `isLoading` becomes `true` when there is an ongoing request and no "loaded data". | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
You can use `isValidating` for indicating there is an ongoing revalidation and `isLoading` for indicating there is nothing to display. | ||||||||
huozhi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
||||||||
<Callout emoji="📝"> | ||||||||
Fallback data and previous data are not considered "loaded data," so when you use fallback data or enable the keepPreviousData option, you might have data to display. | ||||||||
</Callout> | ||||||||
|
||||||||
```jsx | ||||||||
function Stock() { | ||||||||
const { data, isLoading, isValidating } = useSWR(STOCK_API, fetcher, { | ||||||||
refreshInterval: 3000 | ||||||||
}); | ||||||||
|
||||||||
// If it's still loading the initial data, there is nothing to display. | ||||||||
// We return a skeleton here. | ||||||||
if (isLoading) return <div className="skeleton" />; | ||||||||
|
||||||||
// Otherwise, display the data and a spinner that indicates a background | ||||||||
// revalidation. | ||||||||
return ( | ||||||||
<> | ||||||||
<div>${data}</div> | ||||||||
{isValidating ? <div className="spinner" /> : null} | ||||||||
</> | ||||||||
); | ||||||||
} | ||||||||
``` | ||||||||
|
||||||||
 | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
You can find the full code for this example here: https://codesandbox.io/s/swr-isloading-jtopow | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
## Return previous data for better UX | ||||||||
|
||||||||
When doing data fetching based on continuous user actions, e.g. real-time search when typing, keeping the previous fetched data can improve the UX a lot. `keepPreviousData` is an option to enable that behavior. Here's a simple search UI: | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since previously users can also use
isValidating
, we might want to clarify what's the difference.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@huozhi Thank you! I'll create a new section for the difference between
isLoading
andisValidating
on the Understanding SWR page and put the link in the blog post.