reactjs - async rendering of react components, how to set a global Loading component? -


i want render components after successful asynchronous call. before that, want show loading component improve user experience.

in 1 component, know how handle it, referring reactjs async rendering of components. in project, there many pages(components) displaying infos asynchronous call.

so there way set global loading component in place rather write internal state in every component?

maybe take hoc pattern in react: https://facebook.github.io/react/docs/higher-order-components.html

you that:

function withasynchronous(wrappedcomponent, url) {   return class extends react.component {     constructor(props) {       super(props);       this.state = {         isloading: true       };     }      componentdidmount() {       // async call       getasync(url, data => {         this.setstate({ isloading: false, data: data });       });     }      render() {       if (this.state.isloading) {         return (<yourloadingcomponent />);       }        return <wrappedcomponent data={this.state.data} {...this.props} />;     }   }; } 

and render that:

componentdidmount() {   const yourasyncpage = withasynchronous(     yourpage,     '/your/page/url'   ); }  render() {   return (<yourasyncpage />); } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -