From f75c536d3ebe078204aaf5e33167aa403c701b0e Mon Sep 17 00:00:00 2001 From: MoonbamiOfficial <141120384+MoonbamiOfficial@users.noreply.github.com> Date: Mon, 22 Jan 2024 21:04:35 +0800 Subject: [PATCH] feat: add custom hook --- hooks/useFetch.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 hooks/useFetch.ts diff --git a/hooks/useFetch.ts b/hooks/useFetch.ts new file mode 100644 index 0000000..733fca8 --- /dev/null +++ b/hooks/useFetch.ts @@ -0,0 +1,16 @@ +import { Url } from "next/dist/shared/lib/router/router" + +export const useFetch = async (url: Url) => { + try { + const response = await fetch(`${ url }`, { + cache: 'no-store', + }) + + if(!response.ok) { + throw new Error("Failed fetching!") + } + return response.json() + } catch (error) { + console.log("Error occured: ", error) + } +} \ No newline at end of file