mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-12 23:35:38 +08:00
b13e4a3abe
* chore: new articles design and data source * chore: update useSiteData * refactor useSiteData * fix data
36 lines
978 B
TypeScript
36 lines
978 B
TypeScript
/* eslint-disable import/prefer-default-export */
|
|
import * as React from 'react';
|
|
|
|
export function preLoad(list: string[]) {
|
|
if (typeof window !== 'undefined') {
|
|
// 图处预加载;
|
|
const div = document.createElement('div');
|
|
div.style.display = 'none';
|
|
document.body.appendChild(div);
|
|
list.forEach(src => {
|
|
const img = new Image();
|
|
img.src = src;
|
|
div.appendChild(img);
|
|
});
|
|
}
|
|
}
|
|
|
|
export function useSiteData<T>(): [T, boolean] {
|
|
const [data, setData] = React.useState<T>({} as any);
|
|
const [loading, setLoading] = React.useState<boolean>(false);
|
|
|
|
React.useEffect(() => {
|
|
if (Object.keys(data).length === 0 && typeof fetch !== 'undefined') {
|
|
setLoading(true);
|
|
fetch(`https://raw.githubusercontent.com/ant-design/website-data/main/db.json`)
|
|
.then(res => res.json())
|
|
.then(result => {
|
|
setData(result);
|
|
setLoading(false);
|
|
});
|
|
}
|
|
}, []);
|
|
|
|
return [data, loading];
|
|
}
|