mirror of
https://github.com/ragestudio/comty.js.git
synced 2025-06-09 02:24:18 +00:00
update useRequest
This commit is contained in:
parent
7c11af2643
commit
8e26ab1008
@ -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)
|
||||
}]
|
||||
}
|
||||
React.useEffect(() => {
|
||||
makeRequest(...args)
|
||||
}, [])
|
||||
|
||||
return [
|
||||
loading,
|
||||
result,
|
||||
error,
|
||||
(...newArgs) => {
|
||||
setLoading(true)
|
||||
makeRequest(...newArgs)
|
||||
},
|
||||
() => {
|
||||
setLoading(true)
|
||||
makeRequest(...args)
|
||||
},
|
||||
]
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user