update useRequest

This commit is contained in:
SrGooglo 2025-05-21 19:11:12 +00:00
parent 7c11af2643
commit 8e26ab1008

View File

@ -8,6 +8,7 @@ export default (method, ...args) => {
if (typeof method !== "function") { if (typeof method !== "function") {
return [() => {}, null, new Error("Method is not a function"), () => {}] return [() => {}, null, new Error("Method is not a function"), () => {}]
} }
const makeRequest = (...newArgs) => { const makeRequest = (...newArgs) => {
method(...newArgs) method(...newArgs)
.then((data) => { .then((data) => {
@ -15,6 +16,7 @@ export default (method, ...args) => {
setLoading(false) setLoading(false)
}) })
.catch((err) => { .catch((err) => {
console.error(err)
setError(err) setError(err)
setLoading(false) setLoading(false)
}) })
@ -24,8 +26,17 @@ export default (method, ...args) => {
makeRequest(...args) makeRequest(...args)
}, []) }, [])
return [loading, result, error, (...newArgs) => { return [
loading,
result,
error,
(...newArgs) => {
setLoading(true) setLoading(true)
makeRequest(...newArgs) makeRequest(...newArgs)
}] },
() => {
setLoading(true)
makeRequest(...args)
},
]
} }