I run successfully the following code from SWR examples page:
import useSWR from "swr";
const fetcher = url => fetch(url).then(res => res.json());
export default function App() {
const { data, error } = useSWR(
"https://api.github.com/repos/vercel/swr",
fetcher
);
if (error) return "An error has occurred.";
if (!data) return "Loading...";
return (
<div>
<h1>{data.name}</h1>
<p>{data.description}</p>
<strong>{data.subscribers_count}</strong>{" "}
<strong>{data.stargazers_count}</strong>{" "}
<strong>{data.forks_count}</strong>
</div>
);
}
However when I use my custom API and update the properties to be rendered ({data.id}
, {data.title}
...), I get "An error has occurred".
If I remove conditional returns I get "data is undefined":
Unhandled Runtime Error TypeError: data is undefined
I've used my API in other parts of the project and have had no issues.