PricingBlog

Repeatedly fetching API data

  • mace2155-1491886544607252600

    Steve

    1 day ago

    This might be a general coding question but how are people dealing with a scenario where you have for example dashboard, users, teams etc pages and each page has their own API data populating the front end?
    I’m seeing lots of tutorials using onLoad to call an api to then retrieve data. If I then switch back and forth between pages it runs those respective api calls each time. Would it not be better to run all calls once at the highest level (and then pass the retrieved data to the necessary pages/components) when the user logs in for example?

    Curious how people are doing this.. thank you!
  • plusmin-1492022599172096040

    Armand

    15 hours ago

    It depends on what data you need on first load, probably not all data. And when your user updates some of that data, you probably also don't want to refetch everything again right.

    So it will also depend on how much pageswitching (and thus data fetching) is involved in the apps usage.

    I usually ask myself these question:
    1. What do I need onLoad (on each page/component)?
    2. What do I need across several pages/components?
    3. What gets updated the most?
    4. What do I need only once or only in one page/component?

    I'm sure there will be other opinions on and approaches to this.
    👍1