Home
Head's Up: I'm in the middle of upgrading my site. Most things are in place, but there are something missing and/or broken including image alt text. Please bear with me while I'm getting things fixed.

Make Multiple SWR Data Fetch Calls In The Same React Component

This code let's you use multiple swr calls in the same component. The key is to apply a name to each one.

javascript
const fetcher = (url) => fetch(url).then((res) => res.json())
 
const { data: playlists, error: playlistsError } = useSWR('/api/the-playlists', fetcher)
const { data: songs, error: songsError } = useSWR('/api/the-songs', fetcher)

Then you can check for each with :

if (playlistsError) return <div>Could not get playlists</div>
if (!playlists) return <div>Loading playlists...</div>
// do something with playlists


if (songsError) return <div>Could not get songs</div>
if (!songs) return <div>Loading songs...</div>
// do something with songs

Messing with that is left as an exercise for the reader

Note that the useSWR can't go inside a basic function inside a component