1

So I am trying to fetch an API using NextJS and I am using SWR.

My code is as follows:

import useSWR from "swr";
const fetcher = (url: string) => fetch(url).then((res) => res.json());

export default function Home(){


  const { data, error, isLoading } = useSWR(
    "https://api.github.com/repos/vercel/swr",
    fetcher
  );

return <></>
}

When saving the file I get the following message:

error

Ashutosh Dash
  • 344
  • 1
  • 5
  • 11

2 Answers2

0

you are using Next.js 13.4.9. However, the latest version of SWR is 1.2.3, which is compatible with Next.js 13.4.9. So, you can update SWR to the latest version without any problems.

npm install swr@1.2.3

Once you have updated SWR, you should no longer get the error.

Muhammad Idrees
  • 570
  • 4
  • 10
0

The issue gets solved by adding "use client"; at the top.

Refer to: https://swr.vercel.app/docs/with-nextjs

using swr with next

Ashutosh Dash
  • 344
  • 1
  • 5
  • 11