Skip to content

Instantly share code, notes, and snippets.

@shuding
Created November 27, 2019 07:00
Show Gist options
  • Save shuding/8367e914997ed18945762b13e2a0f649 to your computer and use it in GitHub Desktop.
Save shuding/8367e914997ed18945762b13e2a0f649 to your computer and use it in GitHub Desktop.
SWR
// /swr/project.js
import useSWR, { mutate } from 'swr'

export async function fetchProject (id) {
  mutate(`/api/project/${id}`, fetch(`/api/project/${id}`))
}

export default function useProject (id) {
  // don't pass the fetcher so it won't fetch
  return useSWR(`/api/project/${id}`)
}
// components/project.js
function Project ({ id }) {
  const { data } = useProject(id)
  
  return <>
    {data}
    <button onClick={() => fetchProject(id)}>fetch!</button>
  </>
}
@shuding
Copy link
Author

shuding commented Nov 27, 2019

fancy stuff

function useNow (interval) {
  return useSWR('now', () => Date.now(), { refreshInterval: interval, initialData: Date.now() }).data
}

function Clock () {
  const now = useNow(1000)
  return <h1>current time is: {now}</h1>
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment