Skip to content
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

Merged
merged 16 commits into from
Sep 24, 2022
28 changes: 6 additions & 22 deletions pages/blog/swr-v2.en-US.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,19 @@ SWR now uses `useSyncExternalStore` and `startTransition` internally to improve

`isLoading` is a new return value that indicates if there's an ongoing request and no "loaded data". Previously, you have to check `data` and `error` are `undefined`, but now you can use the `isLoading` value.
Copy link
Member

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.

Copy link
Collaborator Author

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 and isValidating on the Understanding SWR page and put the link in the blog post.


You can improve the user experience by combining with `isLoading` and `isValidating` like this.

```jsx
function Stock() {
const { data, isLoading, isValidating } = useSWR(STOCK_API, fetcher, {
refreshInterval: 3000
});
import useSWR from 'swr'

// If it's still loading the initial data, there is nothing to display.
// We return a skeleton here.
if (isLoading) return <div className="skeleton" />;
function Profile() {
const { data, isLoading } = useSWR('/api/user', fetcher)

// Otherwise, display the data and a spinner that indicates a background
// revalidation.
return (
<>
<div>AAPL ${data}</div>
{isValidating ? <div className="spinner" /> : null}
</>
);
if (isLoading) return <div>loading...</div>
return <div>hello {data.name}!</div>
}
```

![An example using the isLoading state](https://user-images.githubusercontent.com/3676859/163695538-9ed6232d-808c-4c83-8606-0dd22bce508d.gif)

You can find the full code for this example here: https://codesandbox.io/s/swr-isloading-v8dfpy.

<Callout emoji="📝">
We have added the new [Understanding SWR](/docs/advanced/understanding) page to describe how SWR returns values.
We have added the new [Understanding SWR](/docs/advanced/understanding) page to describe how SWR returns values, which includes the difference between `isValidating` and `isLoading`, and how to combine them to improve user exeprience.
</Callout>


Expand Down
37 changes: 37 additions & 0 deletions pages/docs/advanced/understanding.en-US.mdx
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
Expand Down Expand Up @@ -42,6 +43,42 @@ This pattern is to fetch data and change the key and revalidate it later with th

![A pattern for key change + previous data + fallback](/img/understanding/key-change-previous-data-fallback.svg)

## Combining with isLoading and isValidating for better UX

`isLoading` and `isValidating` are similar, so you might be confused about which value you should use.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`isLoading` and `isValidating` are similar, so you might be confused about which value you should use.
Comparing to the existing `isValidating` value, `isLoading` is a new property that can help you for the more general loading cases for UX.

`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".
Copy link
Member

Choose a reason for hiding this comment

The 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".
- `isValidating` becomes `true` whenever there is an ongoing request **whatever the the data is loaded or not**
- `isLoading` becomes `true` when there is an ongoing request and **data is not loaded yet**.


You can use `isValidating` for indicating there is an ongoing revalidation and `isLoading` for indicating there is nothing to display.

<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}
</>
);
}
```

![An example using the isLoading state](/img/understanding/isloading.gif)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
![An example using the isLoading state](/img/understanding/isloading.gif)
![An example of using the isLoading state](/img/understanding/isloading.gif)


You can find the full code for this example here: https://codesandbox.io/s/swr-isloading-jtopow
Copy link
Member

Choose a reason for hiding this comment

The 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
You can find the code example [here](https://codesandbox.io/s/swr-isloading-jtopow)


## 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:
Expand Down
Binary file added public/img/understanding/isloading.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.