0

In a class component of react, i am getting one endpoint in props. i need to fetch that endpoint to get exact end points that will be used in links of that component.

I am preferring to save the response in class variables not in state of component. because this data does not impact rendering and user interface.

I want first end point response ready as soon as possible. As per react the best option to call http in ComponentDidMount.

But if i will call this first endpoint in constructor in async way, so this class component will be ready with data earlier and it does not block react rendering. Is it good thing to do? will it have any side effects?

  • 1
    The constructor will finish synchronously, so you can't get something asynchronously there to use that for the first render of the component. Instead you want to get the data in `componentDidMount` and put the result in the component state. – Tholle Mar 17 '19 at 16:54
  • as super is complete, object is created. and if i call axios.get from any line after super, that will run asynchronously. and in then i can set some class variables.why to wait for componentDidMount in this case. – user2000936 Mar 17 '19 at 17:45
  • `axios.get` is always asynchronous, so you can't run it in the constructor and use the result before the constructor is finished however you structure it. – Tholle Mar 17 '19 at 17:49

0 Answers0