Next.js Discord

Discord Forum

useSWR giving error even after following official docs

Answered
Argentine hake posted this in #help-forum
Open in Discord
Avatar
Argentine hakeOP
I have used the following code, which can be found in official docs easily but it throws error
Answered by Risky
your page probably isn't a [client component](https://nextjs-faq.com/when-client-component-in-app-router)
View full answer

12 Replies

Avatar
Argentine hakeOP
import React from "react";
import useSWR from "swr";


const fetcher = (...args) => fetch(...args).then(res => res.json())

export default function App() {
  const { data, error, isLoading } = useSWR(
    "https://rickandmortyapi.com/api/character",
    fetcher
  );

  if (error) return "An error has occurred.";
  if (isLoading) return "Loading...";
  return (
    <>
    Loading...
    </>
  );
}
it throws following error
Image
Avatar
Risky
your page probably isn't a [client component](https://nextjs-faq.com/when-client-component-in-app-router)
Answer
Avatar
Argentine hakeOP
you are right, its working now
thanks dude
I have another question, not related to above issue though
Avatar
Risky
btw you shouldn't make your page a client component tho, instead keep it server and import client into it
can you open a new thread 🙂
Avatar
Argentine hakeOP
yes sir
sure
on it