From e53d7bb7803f950f91a9d185882e38e7eb8eca8f Mon Sep 17 00:00:00 2001 From: Jimmy Liow Date: Wed, 13 Nov 2024 13:52:03 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E5=88=A0=E9=99=A4=E4=B8=8D=E7=94=A8?= =?UTF-8?q?=E7=9A=84=20hook?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/useFormInput.js | 16 ---------- src/hooks/userFetch.js | 61 --------------------------------------- 2 files changed, 77 deletions(-) delete mode 100644 src/hooks/useFormInput.js delete mode 100644 src/hooks/userFetch.js diff --git a/src/hooks/useFormInput.js b/src/hooks/useFormInput.js deleted file mode 100644 index c64ffcf..0000000 --- a/src/hooks/useFormInput.js +++ /dev/null @@ -1,16 +0,0 @@ -import { useState } from 'react' - -export function useFormInput(initialValue) { - const [value, setValue] = useState(initialValue); - - function handleChange(e) { - setValue(e.target.value); - } - - const inputProps = { - value: value, - onChange: handleChange - }; - - return inputProps; -} \ No newline at end of file diff --git a/src/hooks/userFetch.js b/src/hooks/userFetch.js deleted file mode 100644 index c58ae03..0000000 --- a/src/hooks/userFetch.js +++ /dev/null @@ -1,61 +0,0 @@ -import { useEffect, useState } from 'react' - -function checkStatus(response) { - if (response.status >= 200 && response.status < 300) { - return response - } else { - const message = - 'Fetch error: ' + response.url + ' ' + response.status + ' (' + - response.statusText + ')' - const error = new Error(message) - error.response = response - throw error - } -} - -export function useGetJson(url) { - const [data, setData] = useState(null) - useEffect(() => { - if (url) { - let ignore = false - fetch(url) - .then(checkStatus) - .then(response => response.json()) - .then(json => { - if (!ignore) { - setData(json) - } - }) - - return () => { ignore = true } - } - }, [url]) - - return data -} - -export function usePostForm(url, formData) { - const [data, setData] = useState(null) - useEffect(() => { - if (url) { - let ignore = false - fetch(url, { - method: 'POST', - body: formData - }).then(checkStatus) - .then(response => response.json()) - .then(json => { - if (!ignore) { - setData(json) - } - }) - .catch(error => { - throw error - }) - - return () => { ignore = true } - } - }, [url]) - - return data -} \ No newline at end of file