SWR用于数据请求的 React Hooks 库,使用 SWR,组件将会不断地、自动获得最新数据流。UI 也会一直保持快速响应。
yarn add swr 或者 npm install swr
const { data, error, isValidating, mutate } = useSWR(key, fetcher, options)
1.封装useFetch指令
export function useFetch<Data = any, Error = any>(url: string, options: any) { const { data, error, mutate } = useSWR<Data, Error>(url, async (url) => { const response = await staticFetch(url, options); return response; }); return { data, error, mutate }; }
2.使用
const tableData = useFetch(getListInfo, { method: ‘GET’ });