next js data fetching with swr

Solutions on MaxInterview for next js data fetching with swr by the best coders in the world

showing results for - "next js data fetching with swr"
Sarah
17 May 2017
1import useSWR from "swr"
2
3const fetcher = url => fetch(url).then(res => res.json())
4const baseUrl = "https://jsonplaceholder.typicode.com"
5
6export const useGetPosts = path => {
7  if (!path) {
8    throw new Error("Path is required")
9  }
10
11  const url = baseUrl + path
12
13  const { data: posts, error } = useSWR(url, fetcher)
14
15  return { posts, error }
16}