Skip to content

Instantly share code, notes, and snippets.

@ameetmadan
Last active April 2, 2023 08:52
Show Gist options
  • Save ameetmadan/56462ec5b380842633d8b1f598701ffc to your computer and use it in GitHub Desktop.
Save ameetmadan/56462ec5b380842633d8b1f598701ffc to your computer and use it in GitHub Desktop.
Boilerplate code for SWR hook using IntelliJ WebStorm Live Templates
import axios from "axios";
// define the fetcher once, export it and use it every where you use useSWR
export const fetcher = (url: string) => axios.get(url).then(res => res.data)
const YOUR_API_ENDPOINT = '';
const { data, isLoading, error} = useSWR(`${YOUR_API_ENDPOINT}`, fetcher)
if (isLoading) return <div><p>loading</p></div>
if (error) return <div><p>error</p></div>