Wait for data to be fetched in child components, then render











up vote
0
down vote

favorite












I have a React app that uses multiple fetch calls throughout different components. In Home page component, I have imported smaller components, all of whom have it's own fetch call.



render() {
return (
<React.Fragment>
<Banner/>
<Services />
<About />
</React.Fragment>
)
}


Banner, Services and About have their own fetch calls to different endpoints, now my question is because the response is a little bit on the slower side, how to wait for all of the child components to fetch data, then render the Homepage component. I have tried to put the state of isLoading and add a loader to wait for components to fetch, but I don't know what to wait for to set isLoading to false.










share|improve this question






















  • You could pass individual isLoadings for each child, and set them accordingly when they finish fetching.
    – Colin
    Nov 21 at 15:17















up vote
0
down vote

favorite












I have a React app that uses multiple fetch calls throughout different components. In Home page component, I have imported smaller components, all of whom have it's own fetch call.



render() {
return (
<React.Fragment>
<Banner/>
<Services />
<About />
</React.Fragment>
)
}


Banner, Services and About have their own fetch calls to different endpoints, now my question is because the response is a little bit on the slower side, how to wait for all of the child components to fetch data, then render the Homepage component. I have tried to put the state of isLoading and add a loader to wait for components to fetch, but I don't know what to wait for to set isLoading to false.










share|improve this question






















  • You could pass individual isLoadings for each child, and set them accordingly when they finish fetching.
    – Colin
    Nov 21 at 15:17













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a React app that uses multiple fetch calls throughout different components. In Home page component, I have imported smaller components, all of whom have it's own fetch call.



render() {
return (
<React.Fragment>
<Banner/>
<Services />
<About />
</React.Fragment>
)
}


Banner, Services and About have their own fetch calls to different endpoints, now my question is because the response is a little bit on the slower side, how to wait for all of the child components to fetch data, then render the Homepage component. I have tried to put the state of isLoading and add a loader to wait for components to fetch, but I don't know what to wait for to set isLoading to false.










share|improve this question













I have a React app that uses multiple fetch calls throughout different components. In Home page component, I have imported smaller components, all of whom have it's own fetch call.



render() {
return (
<React.Fragment>
<Banner/>
<Services />
<About />
</React.Fragment>
)
}


Banner, Services and About have their own fetch calls to different endpoints, now my question is because the response is a little bit on the slower side, how to wait for all of the child components to fetch data, then render the Homepage component. I have tried to put the state of isLoading and add a loader to wait for components to fetch, but I don't know what to wait for to set isLoading to false.







javascript reactjs fetch






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 15:16









revetinja

239




239












  • You could pass individual isLoadings for each child, and set them accordingly when they finish fetching.
    – Colin
    Nov 21 at 15:17


















  • You could pass individual isLoadings for each child, and set them accordingly when they finish fetching.
    – Colin
    Nov 21 at 15:17
















You could pass individual isLoadings for each child, and set them accordingly when they finish fetching.
– Colin
Nov 21 at 15:17




You could pass individual isLoadings for each child, and set them accordingly when they finish fetching.
– Colin
Nov 21 at 15:17












3 Answers
3






active

oldest

votes

















up vote
2
down vote



accepted











...how to wait for all of the child components to fetch data, then render the Homepage component




You don't. Instead, you move the fetches to the Homepage component's parent, and then have that parent only render the Homepage component when it has all of the necessary information to do so. In React parlance, this is "lifting state up" (e.g., up the hierarchy to the parent).



While you could render the Homepage in a "loading" form, and have it render its child components in a "loading" form, and have the child components call back to the Home page to say they have their information now, that's more complicated than simply lifting the state up to the highest component that actually needs it (so it knows it can render the Homepage).






share|improve this answer




























    up vote
    1
    down vote













    As @TJCrowder mentioned in his answer, You'll need to lift your state up and keep it in the parent component. Make a network request there and pass the data to your child component as props. You can read more about lifting-state-up here



    class YourComponent extends React.Component {
    state = {isLoading: true, isError: false, banner: null, services: null, about: null};

    async componentDidMount() {
    try {
    const [banner, services, about] = await Promise.all([
    // all calls
    ]);
    this.setState({ isLoading: false, banner, services, about });
    } catch (error) {
    this.setState({ isError: true, isLoading: false });
    }

    }

    render() {
    if (this.state.isLoading) {
    return <div>Loading...</div>
    }

    return (
    <React.Fragment>
    <Banner data={this.state.banner} />
    <Services data={this.state.services} />
    <About data={this.state.about} />
    </React.Fragment>
    )
    }
    }





    share|improve this answer




























      up vote
      0
      down vote













      using promises in fetch you could, as suggested, have a isLoaded property state determine whether or not a component should render or not.



      class ShouldRender extends React.Component {
      constructor(props) {
      super(props);
      this.state = {
      data: ,
      isLoaded: false,
      }
      }

      componentDidMount() {
      fetch('http://someresource.com/api/resource')
      .then(res => res.json())
      .then(data => {
      this.state({
      data,
      isLoaded: true,
      });
      })
      }

      render() {
      const { isLoaded } = this.state;
      if (isLoaded) {
      return <MyAwesomeReactComponent />
      }
      return null;
      }
      }


      So once the state is updated it will trigger a rerendering of the component with the new state that will render the if statement true and you're JSX will appear.






      share|improve this answer





















        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














         

        draft saved


        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415128%2fwait-for-data-to-be-fetched-in-child-components-then-render%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        2
        down vote



        accepted











        ...how to wait for all of the child components to fetch data, then render the Homepage component




        You don't. Instead, you move the fetches to the Homepage component's parent, and then have that parent only render the Homepage component when it has all of the necessary information to do so. In React parlance, this is "lifting state up" (e.g., up the hierarchy to the parent).



        While you could render the Homepage in a "loading" form, and have it render its child components in a "loading" form, and have the child components call back to the Home page to say they have their information now, that's more complicated than simply lifting the state up to the highest component that actually needs it (so it knows it can render the Homepage).






        share|improve this answer

























          up vote
          2
          down vote



          accepted











          ...how to wait for all of the child components to fetch data, then render the Homepage component




          You don't. Instead, you move the fetches to the Homepage component's parent, and then have that parent only render the Homepage component when it has all of the necessary information to do so. In React parlance, this is "lifting state up" (e.g., up the hierarchy to the parent).



          While you could render the Homepage in a "loading" form, and have it render its child components in a "loading" form, and have the child components call back to the Home page to say they have their information now, that's more complicated than simply lifting the state up to the highest component that actually needs it (so it knows it can render the Homepage).






          share|improve this answer























            up vote
            2
            down vote



            accepted







            up vote
            2
            down vote



            accepted







            ...how to wait for all of the child components to fetch data, then render the Homepage component




            You don't. Instead, you move the fetches to the Homepage component's parent, and then have that parent only render the Homepage component when it has all of the necessary information to do so. In React parlance, this is "lifting state up" (e.g., up the hierarchy to the parent).



            While you could render the Homepage in a "loading" form, and have it render its child components in a "loading" form, and have the child components call back to the Home page to say they have their information now, that's more complicated than simply lifting the state up to the highest component that actually needs it (so it knows it can render the Homepage).






            share|improve this answer













            ...how to wait for all of the child components to fetch data, then render the Homepage component




            You don't. Instead, you move the fetches to the Homepage component's parent, and then have that parent only render the Homepage component when it has all of the necessary information to do so. In React parlance, this is "lifting state up" (e.g., up the hierarchy to the parent).



            While you could render the Homepage in a "loading" form, and have it render its child components in a "loading" form, and have the child components call back to the Home page to say they have their information now, that's more complicated than simply lifting the state up to the highest component that actually needs it (so it knows it can render the Homepage).







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 21 at 15:17









            T.J. Crowder

            670k11611841279




            670k11611841279
























                up vote
                1
                down vote













                As @TJCrowder mentioned in his answer, You'll need to lift your state up and keep it in the parent component. Make a network request there and pass the data to your child component as props. You can read more about lifting-state-up here



                class YourComponent extends React.Component {
                state = {isLoading: true, isError: false, banner: null, services: null, about: null};

                async componentDidMount() {
                try {
                const [banner, services, about] = await Promise.all([
                // all calls
                ]);
                this.setState({ isLoading: false, banner, services, about });
                } catch (error) {
                this.setState({ isError: true, isLoading: false });
                }

                }

                render() {
                if (this.state.isLoading) {
                return <div>Loading...</div>
                }

                return (
                <React.Fragment>
                <Banner data={this.state.banner} />
                <Services data={this.state.services} />
                <About data={this.state.about} />
                </React.Fragment>
                )
                }
                }





                share|improve this answer

























                  up vote
                  1
                  down vote













                  As @TJCrowder mentioned in his answer, You'll need to lift your state up and keep it in the parent component. Make a network request there and pass the data to your child component as props. You can read more about lifting-state-up here



                  class YourComponent extends React.Component {
                  state = {isLoading: true, isError: false, banner: null, services: null, about: null};

                  async componentDidMount() {
                  try {
                  const [banner, services, about] = await Promise.all([
                  // all calls
                  ]);
                  this.setState({ isLoading: false, banner, services, about });
                  } catch (error) {
                  this.setState({ isError: true, isLoading: false });
                  }

                  }

                  render() {
                  if (this.state.isLoading) {
                  return <div>Loading...</div>
                  }

                  return (
                  <React.Fragment>
                  <Banner data={this.state.banner} />
                  <Services data={this.state.services} />
                  <About data={this.state.about} />
                  </React.Fragment>
                  )
                  }
                  }





                  share|improve this answer























                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    As @TJCrowder mentioned in his answer, You'll need to lift your state up and keep it in the parent component. Make a network request there and pass the data to your child component as props. You can read more about lifting-state-up here



                    class YourComponent extends React.Component {
                    state = {isLoading: true, isError: false, banner: null, services: null, about: null};

                    async componentDidMount() {
                    try {
                    const [banner, services, about] = await Promise.all([
                    // all calls
                    ]);
                    this.setState({ isLoading: false, banner, services, about });
                    } catch (error) {
                    this.setState({ isError: true, isLoading: false });
                    }

                    }

                    render() {
                    if (this.state.isLoading) {
                    return <div>Loading...</div>
                    }

                    return (
                    <React.Fragment>
                    <Banner data={this.state.banner} />
                    <Services data={this.state.services} />
                    <About data={this.state.about} />
                    </React.Fragment>
                    )
                    }
                    }





                    share|improve this answer












                    As @TJCrowder mentioned in his answer, You'll need to lift your state up and keep it in the parent component. Make a network request there and pass the data to your child component as props. You can read more about lifting-state-up here



                    class YourComponent extends React.Component {
                    state = {isLoading: true, isError: false, banner: null, services: null, about: null};

                    async componentDidMount() {
                    try {
                    const [banner, services, about] = await Promise.all([
                    // all calls
                    ]);
                    this.setState({ isLoading: false, banner, services, about });
                    } catch (error) {
                    this.setState({ isError: true, isLoading: false });
                    }

                    }

                    render() {
                    if (this.state.isLoading) {
                    return <div>Loading...</div>
                    }

                    return (
                    <React.Fragment>
                    <Banner data={this.state.banner} />
                    <Services data={this.state.services} />
                    <About data={this.state.about} />
                    </React.Fragment>
                    )
                    }
                    }






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 21 at 15:26









                    Hardik Modha

                    3,80711829




                    3,80711829






















                        up vote
                        0
                        down vote













                        using promises in fetch you could, as suggested, have a isLoaded property state determine whether or not a component should render or not.



                        class ShouldRender extends React.Component {
                        constructor(props) {
                        super(props);
                        this.state = {
                        data: ,
                        isLoaded: false,
                        }
                        }

                        componentDidMount() {
                        fetch('http://someresource.com/api/resource')
                        .then(res => res.json())
                        .then(data => {
                        this.state({
                        data,
                        isLoaded: true,
                        });
                        })
                        }

                        render() {
                        const { isLoaded } = this.state;
                        if (isLoaded) {
                        return <MyAwesomeReactComponent />
                        }
                        return null;
                        }
                        }


                        So once the state is updated it will trigger a rerendering of the component with the new state that will render the if statement true and you're JSX will appear.






                        share|improve this answer

























                          up vote
                          0
                          down vote













                          using promises in fetch you could, as suggested, have a isLoaded property state determine whether or not a component should render or not.



                          class ShouldRender extends React.Component {
                          constructor(props) {
                          super(props);
                          this.state = {
                          data: ,
                          isLoaded: false,
                          }
                          }

                          componentDidMount() {
                          fetch('http://someresource.com/api/resource')
                          .then(res => res.json())
                          .then(data => {
                          this.state({
                          data,
                          isLoaded: true,
                          });
                          })
                          }

                          render() {
                          const { isLoaded } = this.state;
                          if (isLoaded) {
                          return <MyAwesomeReactComponent />
                          }
                          return null;
                          }
                          }


                          So once the state is updated it will trigger a rerendering of the component with the new state that will render the if statement true and you're JSX will appear.






                          share|improve this answer























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            using promises in fetch you could, as suggested, have a isLoaded property state determine whether or not a component should render or not.



                            class ShouldRender extends React.Component {
                            constructor(props) {
                            super(props);
                            this.state = {
                            data: ,
                            isLoaded: false,
                            }
                            }

                            componentDidMount() {
                            fetch('http://someresource.com/api/resource')
                            .then(res => res.json())
                            .then(data => {
                            this.state({
                            data,
                            isLoaded: true,
                            });
                            })
                            }

                            render() {
                            const { isLoaded } = this.state;
                            if (isLoaded) {
                            return <MyAwesomeReactComponent />
                            }
                            return null;
                            }
                            }


                            So once the state is updated it will trigger a rerendering of the component with the new state that will render the if statement true and you're JSX will appear.






                            share|improve this answer












                            using promises in fetch you could, as suggested, have a isLoaded property state determine whether or not a component should render or not.



                            class ShouldRender extends React.Component {
                            constructor(props) {
                            super(props);
                            this.state = {
                            data: ,
                            isLoaded: false,
                            }
                            }

                            componentDidMount() {
                            fetch('http://someresource.com/api/resource')
                            .then(res => res.json())
                            .then(data => {
                            this.state({
                            data,
                            isLoaded: true,
                            });
                            })
                            }

                            render() {
                            const { isLoaded } = this.state;
                            if (isLoaded) {
                            return <MyAwesomeReactComponent />
                            }
                            return null;
                            }
                            }


                            So once the state is updated it will trigger a rerendering of the component with the new state that will render the if statement true and you're JSX will appear.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 21 at 15:24









                            Fotoncito

                            357




                            357






























                                 

                                draft saved


                                draft discarded



















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415128%2fwait-for-data-to-be-fetched-in-child-components-then-render%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                                Calculate evaluation metrics using cross_val_predict sklearn

                                Insert data from modal to MySQL (multiple modal on website)