Skip to content

Commit 5cd0faf

Browse files
authored
docs: use isLoading instead of !data (#2734)
1 parent 54c95ea commit 5cd0faf

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ With SWR, components will get **a stream of data updates constantly and automati
5656
import useSWR from 'swr'
5757

5858
function Profile() {
59-
const { data, error } = useSWR('/api/user', fetcher)
59+
const { data, error, isLoading } = useSWR('/api/user', fetcher)
6060

6161
if (error) return <div>failed to load</div>
62-
if (!data) return <div>loading...</div>
62+
if (isLoading) return <div>loading...</div>
6363
return <div>hello {data.name}!</div>
6464
}
6565
```
@@ -68,9 +68,9 @@ In this example, the React Hook `useSWR` accepts a `key` and a `fetcher` functio
6868
The `key` is a unique identifier of the request, normally the URL of the API. And the `fetcher` accepts
6969
`key` as its parameter and returns the data asynchronously.
7070

71-
`useSWR` also returns 2 values: `data` and `error`. When the request (fetcher) is not yet finished,
72-
`data` will be `undefined`. And when we get a response, it sets `data` and `error` based on the result
73-
of `fetcher` and rerenders the component.
71+
`useSWR` also returns 3 values: `data`, `isLoading` and `error`. When the request (fetcher) is not yet finished,
72+
`data` will be `undefined` and `isLoading` will be `true`. When we get a response, it sets `data` and `error` based on the result
73+
of `fetcher`, `isLoading` to false and rerenders the component.
7474

7575
Note that `fetcher` can be any asynchronous function, you can use your favourite data-fetching
7676
library to handle that part.

core/src/use-swr.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -754,9 +754,9 @@ export { unstable_serialize } from './serialize'
754754
* ```jsx
755755
* import useSWR from 'swr'
756756
* function Profile() {
757-
* const { data, error } = useSWR('/api/user', fetcher)
757+
* const { data, error, isLoading } = useSWR('/api/user', fetcher)
758758
* if (error) return <div>failed to load</div>
759-
* if (!data) return <div>loading...</div>
759+
* if (isLoading) return <div>loading...</div>
760760
* return <div>hello {data.name}!</div>
761761
* }
762762
* ```

0 commit comments

Comments
 (0)