mirror of
https://github.com/ant-design/ant-design.git
synced 2025-06-11 19:42:54 +08:00
22 lines
518 B
TypeScript
22 lines
518 B
TypeScript
import React from 'react';
|
|
import fetch from 'cross-fetch';
|
|
|
|
import FetchCache from './cache';
|
|
|
|
const cache = new FetchCache();
|
|
|
|
const useFetch = <T>(options: string | { request: () => PromiseLike<T>; key: string }) => {
|
|
let request;
|
|
let key;
|
|
if (typeof options === 'string') {
|
|
request = () => fetch(options).then((res) => res.json());
|
|
key = options;
|
|
} else {
|
|
request = options.request;
|
|
key = options.key;
|
|
}
|
|
return React.use<T>(cache.promise<T>(key, request));
|
|
};
|
|
|
|
export default useFetch;
|