简体   繁体   English

如何在渲染组件时添加 Spinner?

[英]how to add Spinner while render a component on react?

suppose,i will use React bootstrap spinner component.假设,我将使用 React bootstrap spinner 组件。 how to show this component while rendering another component like app or anything else.如何在呈现另一个组件(如应用程序或其他任何组件)时显示此组件。

Install react-bootstrap and bootstrap via npm:通过 npm 安装 react-bootstrap 和 bootstrap:

npm install react-bootstrap bootstrap

Call dependency in your component:在您的组件中调用依赖项:

import Spinner from 'react-bootstrap/Spinner';

In your component function you can use this type of code:在您的组件 function 中,您可以使用这种类型的代码:

const [isLoading, setIsLoading] = useState(true)
useEffect(() => {
    setTimeout(() => { // after some fake time, component will stop with render
        setIsLoading(false);
    }, 5000);
}, []);

return (
<>
    {!isLoading && <div>All components loaded.</div>}
    {isLoading && <Spinner animation="border" role="status">
        <span className="visually-hidden">Loading...</span>
    </Spinner>
</>
)

On component render, it will show loading spinner for 5 seconds (5000 ms), and then it should show text: "All components loaded."在组件渲染上,它将显示加载微调器 5 秒(5000 毫秒),然后它应该显示文本:“所有组件已加载。”

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

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