I'm trying to use useSWR in my NEXT.JS app. First, I made a custom hook where I use it :
import useSWR from "swr";
const apiUrl = process.env.NEXT_PUBLIC_API_URL;
const fetcher = (url) => {
fetch(url).then((res) => res.json());
};
export default function useSocialMedia() {
const url = apiUrl + "/socialMedia";
const { data, error } = useSWR(url, fetcher);
console.log("data : ", data);
return {
socialMedia: data,
loading: !error && !data,
error: error,
};
}
Then, I import it in my component :
import styles from "./styles/socialIcons.module.scss";
import useSocialMedia from "../../../utils/hooks/useSocialMedia";
export default function SocialIcons() {
const { socialMedia, loading, error } = useSocialMedia();
return (
<div className={styles.socialIcons}>
{loading && <p>Loading...</p>}
{error && <p>Loading failed : {error}</p>}
{socialMedia && <p>Here will appear my social media list</p>}
</div>
);
}
As a result of my console.log in my custom hook, I always have data undefined and so my component is stuck with loading... I don't understand what's wrong with my code.