San3yuan | 4d0e803 | 2025-04-04 17:21:40 +0800 | [diff] [blame] | 1 | import { useState, useEffect, useCallback } from 'react' |
| 2 | import { data } from 'react-router' |
| 3 | |
| 4 | type RequestFunction<T> = () => Promise<T> |
| 5 | |
| 6 | interface UseApiResult<T> { |
| 7 | data: T | null |
| 8 | loading: boolean |
| 9 | error: Error | null |
| 10 | refresh: () => void |
| 11 | } |
| 12 | |
| 13 | export function useApi<T>( |
| 14 | requestFn: RequestFunction<T>, |
| 15 | immediate = true |
| 16 | ): UseApiResult<T> { |
| 17 | const [data, setData] = useState<T | null>(null) |
| 18 | const [loading, setLoading] = useState(false) |
| 19 | const [error, setError] = useState<Error | null>(null) |
| 20 | |
| 21 | const execute = useCallback(async () => { |
| 22 | try { |
| 23 | setLoading(true) |
| 24 | const result = await requestFn() |
| 25 | setData(result) |
| 26 | setError(null) |
| 27 | return result // 返回请求结果 |
| 28 | } catch (err) { |
| 29 | setError(err as Error) |
| 30 | throw err |
| 31 | } finally { |
| 32 | setLoading(false) |
| 33 | } |
| 34 | }, [requestFn]) |
| 35 | |
| 36 | useEffect(() => { |
| 37 | if (immediate) { |
| 38 | execute() |
| 39 | } |
| 40 | }, [execute, immediate]) |
| 41 | |
| 42 | return { data, loading, error, refresh: execute } |
| 43 | } |