Answers for "next js data fetching with swr"

1

next js data fetching with swr

import useSWR from "swr"

const fetcher = url => fetch(url).then(res => res.json())
const baseUrl = "https://jsonplaceholder.typicode.com"

export const useGetPosts = path => {
  if (!path) {
    throw new Error("Path is required")
  }

  const url = baseUrl + path

  const { data: posts, error } = useSWR(url, fetcher)

  return { posts, error }
}
Posted by: Guest on June-17-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language