Use it today with SWR and TypeScript

kamran
7 min readNov 29, 2020
Photo by Brooke Cagle on Unsplash

Suspense For Data Fetching

Suspense for Data Fetching is an experimental feature in ReactJS. It allows us to “wait” for some code to load, and to declare a loading state while we are waiting. useSWR is a library that takes all the complexity out of data fetching and data caching in React applications. useSWR also supports React Suspense for data fetching.

If you are unfamiliar with useSWR, be sure to check out our previous article on the subject.

useSWR — My New Favorite React Library

Make the hard things easy with React’s best data fetching library

medium.com

Getting started

Let’s start by creating a brand new Create React App. Run the following command.

npx create-react-app --template typescript suspense-swr

After it finishes installing our dependencies and bootstrapping our application, cd into the directory and run yarn add swr.

For this tutorial, we will use Tailwind. For some projects like this, I like to use Tailwind with the CDN. To use Tailwind’s CDN with create react app, simply add the link to the head of your public/index.html.

Now that we have all of our dependencies, we can get started and build this thing.

Writing our hook

We will create a hook using useSWR that returns a user’s profile demo. For the purpose of this tutorial, we will use mock data instead of using an API.

Our mock data

Above you can see our mock data. Now that we have our mock data, we can write our hook.

Above is our hook. We wait 1.5 seconds to simulate network loading and then return our mock profile data.

Notice how we had to create my own TypeScript interface that extends SWR’s response interface? We have to do this because the typings for SWR define data as optional. However, when you are using suspense, the data is not optional, it’s always there. So we need to cast our response to this custom interface to “correct” the typings.

Using our hook

Now that we have defined our hook, we can create our Profile component. We created this component using the Card example in the tailwind docs. Take a look at the code below.

As you can see, our profile data is always defined in this component, we do not need to check for null or to present a loading state.

Below is a screenshot of our Profile component with our mock data. It’s amazing how quickly and easily we can make components using Tailwind’s utility classes.

What our Profile looks like

Next, let’s define a component called ProfileSkeleton. This component will be shown to our users while our data is fetching.

https://ceds.ed.gov/ppv/bal-v-nal-h01.html
https://ceds.ed.gov/ppv/bal-v-nal-h02.html
https://ceds.ed.gov/ppv/bal-v-nal-h03.html
https://ceds.ed.gov/ppv/bal-v-nal-h04.html
https://ceds.ed.gov/ppv/bal-v-nal-h05.html
https://ceds.ed.gov/ppv/bal-v-nal-h06.html
https://www.un.org/sites/www.iamladp.org/files/webform/bal-v-nal-h01.html
https://www.un.org/sites/www.iamladp.org/files/webform/bal-v-nal-h02.html
https://www.un.org/sites/www.iamladp.org/files/webform/bal-v-nal-h03.html
https://www.un.org/sites/www.iamladp.org/files/webform/bal-v-nal-h04.html
https://www.un.org/sites/www.iamladp.org/files/webform/bal-v-nal-h05.html
https://www.un.org/sites/www.iamladp.org/files/webform/bal-v-nal-h06.html
https://www.omicsonline.org/open-access-pdfs/kyir/jala/bal-v-nal-h01.html
https://www.omicsonline.org/open-access-pdfs/kyir/jala/bal-v-nal-h02.html
https://www.omicsonline.org/open-access-pdfs/kyir/jala/bal-v-nal-h03.html
https://www.omicsonline.org/open-access-pdfs/kyir/jala/bal-v-nal-h04.html
https://www.omicsonline.org/open-access-pdfs/kyir/jala/bal-v-nal-h05.html
https://www.omicsonline.org/open-access-pdfs/kyir/jala/bal-v-nal-h06.html
http://admin.maps.bpex.org.uk/bll/cll/SC-v-G00.html
http://admin.maps.bpex.org.uk/bll/cll/SC-v-G01.html
http://admin.maps.bpex.org.uk/bll/cll/SC-v-G02.html
http://admin.maps.bpex.org.uk/bll/cll/SC-v-G03.html
http://admin.maps.bpex.org.uk/bll/cll/SC-v-G04.html
http://admin.maps.bpex.org.uk/bll/cll/SC-v-G05.html
http://admin.maps.bpex.org.uk/bll/cll/Texas-v-LSU-liv-lix00.html
http://admin.maps.bpex.org.uk/bll/cll/Texas-v-LSU-liv-lix01.html
http://admin.maps.bpex.org.uk/bll/cll/Texas-v-LSU-liv-lix02.html
http://admin.maps.bpex.org.uk/bll/cll/Texas-v-LSU-liv-lix03.html
http://admin.maps.bpex.org.uk/bll/cll/Texas-v-LSU-liv-lix04.html
http://admin.maps.bpex.org.uk/bll/dll/Box-live00.html
http://admin.maps.bpex.org.uk/bll/dll/Box-live01.html
http://admin.maps.bpex.org.uk/bll/dll/Box-live02.html
http://admin.maps.bpex.org.uk/bll/dll/Box-live03.html
http://admin.maps.bpex.org.uk/bll/dll/Box-live04.html
http://admin.maps.bpex.org.uk/bll/dll/Box-live05.html
http://admin.maps.bpex.org.uk/bll/dll/Box-live06.html
http://admin.maps.bpex.org.uk/bll/dll/Box-live07.html
http://admin.maps.bpex.org.uk/bll/dll/Box-live08.html
http://admin.maps.bpex.org.uk/bll/dll/Box-live09.html
http://admin.maps.bpex.org.uk/bll/dll/R-v-T0.html
http://admin.maps.bpex.org.uk/bll/dll/R-v-T1.html
http://admin.maps.bpex.org.uk/bll/dll/R-v-T2.html
http://admin.maps.bpex.org.uk/bll/dll/R-v-T3.html
http://admin.maps.bpex.org.uk/bll/dll/R-v-T4.html
http://admin.maps.bpex.org.uk/bll/dll/R-v-T5.html
https://www.hotel-alley.com/bll/dll/Box-live00.html
https://www.hotel-alley.com/bll/dll/Box-live01.html
https://www.hotel-alley.com/bll/dll/Box-live02.html
https://www.hotel-alley.com/bll/dll/Box-live03.html
https://www.hotel-alley.com/bll/dll/Box-live04.html
https://www.hotel-alley.com/bll/dll/Box-live05.html
https://www.hotel-alley.com/bll/dll/Box-live06.html
https://www.hotel-alley.com/bll/dll/Box-live07.html
https://www.hotel-alley.com/bll/dll/Box-live08.html
https://www.hotel-alley.com/bll/dll/Box-live09.html
https://www.hotel-alley.com/bll/dll/R-v-T0.html
https://www.hotel-alley.com/bll/dll/R-v-T1.html
https://www.hotel-alley.com/bll/dll/R-v-T2.html
https://www.hotel-alley.com/bll/dll/R-v-T3.html
https://www.hotel-alley.com/bll/dll/R-v-T4.html
https://www.hotel-alley.com/bll/dll/R-v-T5.html
https://www.hotel-alley.com/bll/cll/SC-v-G00.html
https://www.hotel-alley.com/bll/cll/SC-v-G01.html
https://www.hotel-alley.com/bll/cll/SC-v-G02.html
https://www.hotel-alley.com/bll/cll/SC-v-G03.html
https://www.hotel-alley.com/bll/cll/SC-v-G04.html
https://www.hotel-alley.com/bll/cll/SC-v-G05.html
https://www.hotel-alley.com/bll/cll/Texas-v-LSU-liv-lix00.html
https://www.hotel-alley.com/bll/cll/Texas-v-LSU-liv-lix01.html
https://www.hotel-alley.com/bll/cll/Texas-v-LSU-liv-lix02.html
https://www.hotel-alley.com/bll/cll/Texas-v-LSU-liv-lix03.html
https://www.hotel-alley.com/bll/cll/Texas-v-LSU-liv-lix04.html
https://shawee.io/pdp/Box-live00.html
https://shawee.io/pdp/Box-live01.html
https://shawee.io/pdp/Box-live02.html
https://shawee.io/pdp/Box-live03.html
https://shawee.io/pdp/Box-live04.html
https://shawee.io/pdp/Box-live05.html
https://shawee.io/pdp/Box-live06.html
https://shawee.io/pdp/Box-live07.html
https://shawee.io/pdp/Box-live08.html
https://shawee.io/pdp/Box-live09.html
https://shawee.io/pdp/R-v-T0.html
https://shawee.io/pdp/R-v-T1.html
https://shawee.io/pdp/R-v-T2.html
https://shawee.io/pdp/R-v-T3.html
https://shawee.io/pdp/R-v-T4.html
https://shawee.io/pdp/R-v-T5.html
http://admin.maps.bpex.org.uk/bll/cll/Rupa-Car-v-Geo-j00.html
http://admin.maps.bpex.org.uk/bll/cll/Rupa-Car-v-Geo-j01.html
http://admin.maps.bpex.org.uk/bll/cll/Rupa-Car-v-Geo-j02.html
http://admin.maps.bpex.org.uk/bll/cll/Rupa-Car-v-Geo-j03.html
http://admin.maps.bpex.org.uk/bll/dll/d-v-j00.html
http://admin.maps.bpex.org.uk/bll/dll/d-v-j01.html
http://admin.maps.bpex.org.uk/bll/dll/d-v-j02.html
http://admin.maps.bpex.org.uk/bll/dll/d-v-j03.html
http://admin.maps.bpex.org.uk/bll/dll/Sexy-taifur-Mik-Tyso-v-Ro-Jon-Jr-box-jp-tv.html
http://admin.maps.bpex.org.uk/bll/dll/Sexy-taifur-Mik-Tyso-v-Ro-Jon-Jr-box-jp-tv1.html
http://admin.maps.bpex.org.uk/bll/dll/Sexy-taifur-Mik-Tyso-v-Ro-Jon-Jr-box-jp-tv2.html
http://admin.maps.bpex.org.uk/bll/dll/Sexy-taifur-Mik-Tyso-v-Ro-Jon-Jr-box-jp-tv3.html
http://admin.maps.bpex.org.uk/bll/dll/Sexy-taifur-Mik-Tyso-v-Ro-Jon-Jr-box-jp-tv4.html
http://admin.maps.bpex.org.uk/bll/dll/t-v-r00.html
http://admin.maps.bpex.org.uk/bll/dll/t-v-r01.html
http://admin.maps.bpex.org.uk/bll/dll/t-v-r02.html
http://admin.maps.bpex.org.uk/bll/dll/t-v-r03.html
https://www.hotel-alley.com/bll/cll/Lopa-Tex-v-LSU-j00.html
https://www.hotel-alley.com/bll/cll/Lopa-Tex-v-LSU-j01.html
https://www.hotel-alley.com/bll/cll/Lopa-Tex-v-LSU-j02.html
https://www.hotel-alley.com/bll/cll/Lopa-Tex-v-LSU-j03.html
https://www.hotel-alley.com/bll/cll/Rupa-Car-v-Geo-j00.html
https://www.hotel-alley.com/bll/cll/Rupa-Car-v-Geo-j01.html
https://www.hotel-alley.com/bll/cll/Rupa-Car-v-Geo-j02.html
https://www.hotel-alley.com/bll/cll/Rupa-Car-v-Geo-j03.html
https://www.hotel-alley.com/bll/dll/d-v-j00.html
https://www.hotel-alley.com/bll/dll/d-v-j01.html
https://www.hotel-alley.com/bll/dll/d-v-j02.html
https://www.hotel-alley.com/bll/dll/d-v-j03.html
https://www.hotel-alley.com/bll/dll/Sexy-taifur-Mik-Tyso-v-Ro-Jon-Jr-box-jp-tv.html
https://www.hotel-alley.com/bll/dll/Sexy-taifur-Mik-Tyso-v-Ro-Jon-Jr-box-jp-tv1.html
https://www.hotel-alley.com/bll/dll/Sexy-taifur-Mik-Tyso-v-Ro-Jon-Jr-box-jp-tv2.html
https://www.hotel-alley.com/bll/dll/Sexy-taifur-Mik-Tyso-v-Ro-Jon-Jr-box-jp-tv3.html
https://www.hotel-alley.com/bll/dll/Sexy-taifur-Mik-Tyso-v-Ro-Jon-Jr-box-jp-tv4.html
https://www.hotel-alley.com/bll/dll/t-v-r00.html
https://www.hotel-alley.com/bll/dll/t-v-r01.html
https://www.hotel-alley.com/bll/dll/t-v-r02.html
https://www.hotel-alley.com/bll/dll/t-v-r03.html
http://admin.maps.bpex.org.uk/bll/cll/Lopa-Tex-v-LSU-j00.html
http://admin.maps.bpex.org.uk/bll/cll/Lopa-Tex-v-LSU-j01.html
http://admin.maps.bpex.org.uk/bll/cll/Lopa-Tex-v-LSU-j02.html
http://admin.maps.bpex.org.uk/bll/cll/Lopa-Tex-v-LSU-j03.html
https://shawee.io/pdp/d-v-j00.html
https://shawee.io/pdp/d-v-j01.html
https://shawee.io/pdp/d-v-j02.html
https://shawee.io/pdp/d-v-j03.html
https://shawee.io/pdp/Sexy-taifur-Mik-Tyso-v-Ro-Jon-Jr-box-jp-tv.html
https://shawee.io/pdp/Sexy-taifur-Mik-Tyso-v-Ro-Jon-Jr-box-jp-tv1.html
https://shawee.io/pdp/Sexy-taifur-Mik-Tyso-v-Ro-Jon-Jr-box-jp-tv2.html
https://shawee.io/pdp/Sexy-taifur-Mik-Tyso-v-Ro-Jon-Jr-box-jp-tv3.html
https://shawee.io/pdp/t-v-r00.html
https://shawee.io/pdp/t-v-r01.html
https://shawee.io/pdp/t-v-r02.html
https://shawee.io/pdp/t-v-r03.html
http://admin.maps.bpex.org.uk/bll/dll/M-v-R-fi1.html
http://admin.maps.bpex.org.uk/bll/dll/M-v-R-fi2.html
http://admin.maps.bpex.org.uk/bll/dll/M-v-R-fi3.html
http://admin.maps.bpex.org.uk/bll/dll/M-v-R-fi4.html
http://admin.maps.bpex.org.uk/bll/dll/M-v-R-fi5.html
http://admin.maps.bpex.org.uk/bll/dll/M-v-R-fi6.html
http://admin.maps.bpex.org.uk/bll/dll/M-v-R-fi7.html
https://www.hotel-alley.com/bll/dll/M-v-R-fi1.html
https://www.hotel-alley.com/bll/dll/M-v-R-fi2.html
https://www.hotel-alley.com/bll/dll/M-v-R-fi3.html
https://www.hotel-alley.com/bll/dll/M-v-R-fi4.html
https://www.hotel-alley.com/bll/dll/M-v-R-fi5.html
https://www.hotel-alley.com/bll/dll/M-v-R-fi6.html
https://www.hotel-alley.com/bll/dll/M-v-R-fi7.html
https://www.hotel-alley.com/bll/cll/Haw-v-Nev-nc-01.html
https://www.hotel-alley.com/bll/cll/Haw-v-Nev-nc-02.html
https://www.hotel-alley.com/bll/cll/Haw-v-Nev-nc-03.html
https://www.hotel-alley.com/bll/cll/Haw-v-Nev-nc-04.html
https://www.hotel-alley.com/bll/cll/Haw-v-Nev-nc-05.html
https://www.hotel-alley.com/bll/cll/Haw-v-Nev-nc-06.html
https://www.hotel-alley.com/bll/cll/Haw-v-Nev-nc-07.html
https://www.hotel-alley.com/bll/cll/Haw-v-Nev-nc-08.html
https://www.hotel-alley.com/bll/cll/Haw-v-Nev-nc-09.html
http://admin.maps.bpex.org.uk/bll/cll/Haw-v-Nev-nc-01.html
http://admin.maps.bpex.org.uk/bll/cll/Haw-v-Nev-nc-02.html
http://admin.maps.bpex.org.uk/bll/cll/Haw-v-Nev-nc-03.html
http://admin.maps.bpex.org.uk/bll/cll/Haw-v-Nev-nc-04.html
http://admin.maps.bpex.org.uk/bll/cll/Haw-v-Nev-nc-05.html
http://admin.maps.bpex.org.uk/bll/cll/Haw-v-Nev-nc-06.html
http://admin.maps.bpex.org.uk/bll/cll/Haw-v-Nev-nc-07.html
http://admin.maps.bpex.org.uk/bll/cll/Haw-v-Nev-nc-08.html
http://admin.maps.bpex.org.uk/bll/cll/Haw-v-Nev-nc-09.html
https://shawee.io/pdp/M-v-R-fi1.html
https://shawee.io/pdp/M-v-R-fi2.html
https://shawee.io/pdp/M-v-R-fi3.html
https://shawee.io/pdp/M-v-R-fi4.html
https://shawee.io/pdp/M-v-R-fi5.html
https://shawee.io/pdp/M-v-R-fi6.html
https://shawee.io/pdp/M-v-R-fi7.html

Our ProfileSkeleton is similar to our Profile component, except we replace the profile image with a div with bottom padding. Below you can see a screenshot of our Skeleton component.

What our ProfileSkeleton looks like

Putting it together

Now that we have our data fetching hook, our Profile component, and our Skeleton component, we can put it all together.

Our application in action!

NOTE* NextJS users will need to check if they are on the server before rendering a Suspense element, as React-Dom-Server does not yet support suspense. Below is an example of using Suspense with NextJS.

NextJS example

That’s it! If you would like to see all the code, you can check it out on Github.

Thanks for taking the time to read this tutorial and for continuing to support Frontend Digest. As always, be sure to let us know in the comments if you have any questions.

--

--