简体   繁体   English

反应-在渲染DOM时加载屏幕吗?

[英]React - Loading screen while DOM is rendering?

I want to display the loading screen till the DOM is rendered. 我想显示加载屏幕,直到呈现DOM。 In my program. 在我的程序中。 I am checking if there are devices then it should show the loading page till DOM is rendered else show no devices found. 我正在检查是否有设备,然后它将显示加载页面,直到呈现DOM为止,否则显示未找到任何设备。

But the page shows No devices found then after the DOM is rendered it shows the devices. 但是页面显示未找到任何设备,然后在呈现DOM之后显示设备。 How can I fix this and show the "Loading" message if there are devices before the DOM is rendered. 如果存在渲染DOM之前的设备,如何解决此问题并显示“正在加载”消息。

It takes time to render the devices as well. 渲染设备也需要时间。 So I cannot check if there are devices - thats the reason it goes to No devices found first. 因此,我无法检查是否有设备-这就是“首先找不到设备”的原因。

render() {
    const { devices } = this.props;

    if (this.state.isLoading) {
        return (
            <div>Loading!!!!!</div>
        );
    }
    if (devices && devices.length > 0) {
        return (
            <div> // Devices table will be displayed
            </div>
        );
    } else {
        return (
            <div>
                No Devices found
            </div>
        );
    }
}
render() {
    const { devices } = this.props;

      if (devices && devices.length > 0) {
          return (
                <div> // Devices table will be displayed
                </div>
        );
       }else {
if(this.state.loading){                   //Not sure how you set the state
return ( <div>Loading...</div>)  
}

        return (
            <div>
                No Devices found
            </div>
        );
    }
}

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

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