import { message } from 'antd'; import { useState } from 'react'; import { useEvent } from './useEvent'; export function useRequest(queryFn: (...args: P[]) => Promise) { const [loading, setLoading] = useState(false); const [data, setData] = useState(undefined); const run = useEvent(async (...args: P[]) => { try { setLoading(true); const res = await queryFn(...args); setData(res); } catch (err: any) { message.error(err.message ?? String(err)); } finally { setLoading(false); } }); return [ { loading, data, }, run, ] as const; }