简体   繁体   中英

React - Loading screen while DOM is rendering?

I want to display the loading screen till the DOM is rendered. 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.

But the page shows No devices found then after the DOM is rendered it shows the devices. How can I fix this and show the "Loading" message if there are devices before the DOM is rendered.

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>
        );
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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