From 75ac9e792ab3fa90f9858edafe16861e84722111 Mon Sep 17 00:00:00 2001 From: skidoodle Date: Thu, 20 Oct 2022 23:55:52 +0200 Subject: [PATCH] use swr --- pages/index.tsx | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/pages/index.tsx b/pages/index.tsx index 0739139..ca7436d 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -1,6 +1,7 @@ import Image from "next/image"; import Link from "next/link"; -import React, { useEffect } from "react"; +import React from "react"; +import useSWR from "swr"; import FadeIn from "react-fade-in"; import { socials } from "components/data/socials"; @@ -9,26 +10,14 @@ import { Toaster } from "react-hot-toast"; import { FaSpotify } from "react-icons/fa"; import profilePic from "public/profile.webp"; -import { useState } from "react"; + +export const fetcher = (url: RequestInfo) => fetch(url).then((r) => r.json()); export default function () { - const [spotify, setSpotify] = useState(""); - - const handleData = () => { - fetch("/api/spotify") - .then((res) => res.json()) - .then((data) => setSpotify(data)); - }; - - useEffect(() => { - handleData(); - - return () => { - setInterval(() => { - handleData(); - }, 3000); - }; - }, []); + var { data: spotify } = useSWR("/api/spotify", fetcher, { + refreshInterval: 3000, + fallbackData: "loading", + }); return (