How to use SWR

In a Next.js app, one of the best ways to do a GET request is to use SWR.

You install it with

npm install swr

and you have to define a fetcher function, I always use the same in a lib/fetcher.js file:

const fetcher = (...args) => fetch(...args).then((res) => res.json())
export default fetcher

You import it at the top of your component’s file:

import fetcher from 'lib/fetcher'

Then you can start using it.

At the top of a component, import useSWR:

import useSWR from 'swr'

Then inside the component, at the top, we call useSWR to load the data we need:

const { data } = useSWR(`/api/data`, fetcher)

In addition to the data property, the object returned from useSWR contains isLoading and isError. isLoading is especially useful to show some kind of “loading…” visual indication.

You can pass an additional object to useSWR with some options, for example I use this to limit the number of revalidation SWR does, so I don’t get repeated connections to the endpoint when I’m in development mode:

const { data } = useSWR(`/api/data`, fetcher, {
  revalidateOnFocus: false,
  revalidateOnReconnect: false,
  refreshWhenOffline: false,
  refreshWhenHidden: false,
  refreshInterval: 0
})


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

In a Next.js app, one of the best ways to do a GET request is to use SWR.

You install it with

npm install swr

and you have to define a fetcher function, I always use the same in a lib/fetcher.js file:

const fetcher = (...args) => fetch(...args).then((res) => res.json())
export default fetcher

You import it at the top of your component’s file:

import fetcher from 'lib/fetcher'

Then you can start using it.

At the top of a component, import useSWR:

import useSWR from 'swr'

Then inside the component, at the top, we call useSWR to load the data we need:

const { data } = useSWR(`/api/data`, fetcher)

In addition to the data property, the object returned from useSWR contains isLoading and isError. isLoading is especially useful to show some kind of “loading…” visual indication.

You can pass an additional object to useSWR with some options, for example I use this to limit the number of revalidation SWR does, so I don’t get repeated connections to the endpoint when I’m in development mode:

const { data } = useSWR(`/api/data`, fetcher, {
  revalidateOnFocus: false,
  revalidateOnReconnect: false,
  refreshWhenOffline: false,
  refreshWhenHidden: false,
  refreshInterval: 0
})


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-07-24T05:00:00+00:00) How to use SWR. Retrieved from https://www.scien.cx/2021/07/24/how-to-use-swr/

MLA
" » How to use SWR." flaviocopes.com | Sciencx - Saturday July 24, 2021, https://www.scien.cx/2021/07/24/how-to-use-swr/
HARVARD
flaviocopes.com | Sciencx Saturday July 24, 2021 » How to use SWR., viewed ,<https://www.scien.cx/2021/07/24/how-to-use-swr/>
VANCOUVER
flaviocopes.com | Sciencx - » How to use SWR. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/24/how-to-use-swr/
CHICAGO
" » How to use SWR." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/07/24/how-to-use-swr/
IEEE
" » How to use SWR." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/07/24/how-to-use-swr/. [Accessed: ]
rf:citation
» How to use SWR | flaviocopes.com | Sciencx | https://www.scien.cx/2021/07/24/how-to-use-swr/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.