mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-15 17:19:11 +08:00
d1f8b500df
* perf: better async component * feat: useFetch * chore: code clean * chore: code clean * chore: fix lint * type: enhance
21 lines
507 B
TypeScript
21 lines
507 B
TypeScript
import fetch from 'cross-fetch';
|
|
import use from '../use';
|
|
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 use(cache.promise<T>(key, request));
|
|
};
|
|
|
|
export default useFetch;
|