From 8e26ab100826c5c34de67898c06b0dc94dfed6b7 Mon Sep 17 00:00:00 2001 From: SrGooglo Date: Wed, 21 May 2025 19:11:12 +0000 Subject: [PATCH] update useRequest --- src/hooks/useRequest/index.js | 61 +++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/src/hooks/useRequest/index.js b/src/hooks/useRequest/index.js index 8b7ab7a..59e84c6 100755 --- a/src/hooks/useRequest/index.js +++ b/src/hooks/useRequest/index.js @@ -1,31 +1,42 @@ import React from "react" export default (method, ...args) => { - const [loading, setLoading] = React.useState(true) - const [result, setResult] = React.useState(null) - const [error, setError] = React.useState(null) + const [loading, setLoading] = React.useState(true) + const [result, setResult] = React.useState(null) + const [error, setError] = React.useState(null) - if (typeof method !== "function") { - return [() => {}, null, new Error("Method is not a function"), () => {}] - } - const makeRequest = (...newArgs) => { - method(...newArgs) - .then((data) => { - setResult(data) - setLoading(false) - }) - .catch((err) => { - setError(err) - setLoading(false) - }) - } + if (typeof method !== "function") { + return [() => {}, null, new Error("Method is not a function"), () => {}] + } - React.useEffect(() => { - makeRequest(...args) - }, []) + const makeRequest = (...newArgs) => { + method(...newArgs) + .then((data) => { + setResult(data) + setLoading(false) + }) + .catch((err) => { + console.error(err) + setError(err) + setLoading(false) + }) + } - return [loading, result, error, (...newArgs) => { - setLoading(true) - makeRequest(...newArgs) - }] -} \ No newline at end of file + React.useEffect(() => { + makeRequest(...args) + }, []) + + return [ + loading, + result, + error, + (...newArgs) => { + setLoading(true) + makeRequest(...newArgs) + }, + () => { + setLoading(true) + makeRequest(...args) + }, + ] +}