简体   繁体   English

在 react.js 中从 api 完全获取数据之前加载骨架

[英]Loading Skeleton before data totally fetched from api in react.js

I want to Load Skeleton 10 times before data totally fetched from API in react.js with the following code, but the skeleton not loading...我想使用以下代码在 react.js 中从 API 完全获取数据之前加载骨架 10 次,但骨架未加载...

const snippetsdata = snippetsData.data;;
const [snippets, setSnippets] = useState([]);
const [loading, setLoading] = useState(false);

useEffect(() => {
    setLoading(true);
    const timer = setTimeout(() => {
        setSnippets(snippetsdata);
    setLoading(false);
    }, 3000);
    return () => clearTimeout(timer);
}, []);
{loading && Array(10).map(()=>{
                                <SnippetsSkeleton />
                            })}

When you use Array(10) actually your map function doesn't iterate over your array items because Array(10) creates an array with a length of 10 but without any elements, so the indices [0] , [1] , ..., [9] is not created.当您使用Array(10)实际上您的map function 不会迭代您的数组项,因为Array(10)创建一个长度为 10 但没有任何元素的数组,因此索引[0][1] , .. ., [9]未创建。 Instead of Array(10).map(...) you can use Array.from , like this:您可以使用Array.from代替Array(10).map(...) ,如下所示:

Array.from({length: 10}, (v,i)=> <SnippetsSkeleton key={i}/>)

or或者

Array.from(Array(10), (v,i)=> <SnippetsSkeleton key={i}/>)

Another solution to solve your is using Array.fill , like this:解决您的另一个解决方案是使用Array.fill ,如下所示:

Array(10).fill(undefined).map((v,i)=> <SnippetsSkeleton key={i}/>);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM