skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

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

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

    using swr with next


  2. 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 [email protected]
    

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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search