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) + }, + ] +}