@@ -56,10 +56,10 @@ With SWR, components will get **a stream of data updates constantly and automati
56
56
import useSWR from ' swr'
57
57
58
58
function Profile () {
59
- const { data , error } = useSWR (' /api/user' , fetcher)
59
+ const { data , error , isLoading } = useSWR (' /api/user' , fetcher)
60
60
61
61
if (error) return < div> failed to load< / div>
62
- if (! data ) return < div> loading... < / div>
62
+ if (isLoading ) return < div> loading... < / div>
63
63
return < div> hello {data .name }! < / div>
64
64
}
65
65
```
@@ -68,9 +68,9 @@ In this example, the React Hook `useSWR` accepts a `key` and a `fetcher` functio
68
68
The ` key ` is a unique identifier of the request, normally the URL of the API. And the ` fetcher ` accepts
69
69
` key ` as its parameter and returns the data asynchronously.
70
70
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.
74
74
75
75
Note that ` fetcher ` can be any asynchronous function, you can use your favourite data-fetching
76
76
library to handle that part.
0 commit comments