Skip to content

Commit

Permalink
Update docs to explain dependent fetching with falsy (#449)
Browse files Browse the repository at this point in the history
* Update docs to explain dependent fetching with falsy

This doesn't appear to be well documented. (See #426.)

* Update README.md

Co-authored-by: Shu Ding <ds303077135@gmail.com>

Co-authored-by: Shu Ding <ds303077135@gmail.com>
  • Loading branch information
joshkel and shuding committed Jun 11, 2020
1 parent b7891a0 commit cda50a5
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,11 @@ SWR also allows you to fetch data that depends on other data. It ensures the max
function MyProjects () {
const { data: user } = useSWR('/api/user')
const { data: projects } = useSWR(() => '/api/projects?uid=' + user.id)
// When passing a function, SWR will use the
// return value as `key`. If the function throws,
// SWR will know that some dependencies are not
// ready. In this case it is `user`.
// When passing a function, SWR will use the return
// value as `key`. If the function throws or returns
// falsy, SWR will know that some dependencies are not
// ready. In this case `user.id` throws when `user`
// isn't loaded.

if (!projects) return 'loading...'
return 'You have ' + projects.length + ' projects'
Expand Down

0 comments on commit cda50a5

Please sign in to comment.