0

In ReactJS if I have components that get data from an API(it was called inside ComponentDidMount lifecycle) and I use the component multiple times, does it call 5 times as well? Note: Same API, Same URL all is same I just used it multiple times.

Because if it called 5 times does it will be more efficient if I call the API outside the components and just feed the data so it will only be called once?

Hawk
  • 514
  • 1
  • 7
  • 22
  • 1
    Is it more efficient if I just call the API at the constructor at the top level(app level) and feed the feed to the components? Hmmm – Hawk Sep 09 '18 at 05:19
  • This will help you to decide whether to call api in constructor or componentdidmount https://stackoverflow.com/questions/52092341/how-should-i-alternate-componentwillmount/52092495#52092495 – Bhojendra Rauniyar Sep 09 '18 at 05:52

1 Answers1

0

Yes, It's calling the data in every single component's componentDidMount() event and for this reason, If the data, URL, and everything will be same, you need to call it in parent component and pass data to child components using props or redux.

Pouya Jabbarisani
  • 1,084
  • 3
  • 16
  • 29