Pricing Blog

How can I call an API inside a component from my main page?

  • gerson_ca-1368377116848160919

    Gerson CA

    2 months ago

    I moved my API to a component but I want to know if I can call it from my main page, or another component, after an API runs successfully.
  • Tod-1368377118928408677

    Tod

    2 months ago

    Great job @Gerson CA! Your contribution to the Nordcraft Community just made you advance to Community Level 3! 🌲
  • lucasg-1368408525067517962

    Lucas G

    2 months ago

    You could use attributes and the onAttributeChange workflow
  • Or workflows/contexts. It depends on the hierarchy
  • gerson_ca-1368411188781908040

    Gerson CA

    2 months ago

    Thanks @Lucas G it’s a one level component so I can use the attributes but how could I change the attribute value after the API runs successfully on my main page?
  • lucasg-1368412921755078687

    Lucas G

    2 months ago

    You could pass in some value from the API call to the attribute
  • Anything really
  • The value itself doesn't matter, you're just using it to trigger the workflow
  • gerson_ca-1368416380616970402

    Gerson CA

    2 months ago

    Oh I see got it and do you think it would work also if I want to call that same API from another component instead of my main page?
  • lucasg-1368422701068320780

    Lucas G

    2 months ago

    Yes, the workflow would work the same regardless of where the component is used
  • gerson_ca-1368428225176408074

    Gerson CA

    2 months ago

    Even if the components are not nested?
  • lucasg-1368434687927783464

    Lucas G

    2 months ago

    To clarify, are you referring to the API inside the component?
  • The API inside the component could be triggered by any change to an attribute
  • The attribute could be just 'trigger' or whatever you want to call it.
  • That said, if possible, you may want to move the API up to a higher level and simply pass down the data to the component via context
  • gerson_ca-1368436028385398854

    Gerson CA

    2 months ago

    Yeah the thing is that I have this SPA and I’m making each section a component and because of that I wanted to know if I can call API’s between components that are not nested. However, seems that it would be better just to leave them in the main page
  • lucasg-1368440317224943687

    Lucas G

    2 months ago

    There are different ways to go about it
  • You don't have to necessarily leave them in the main page. The APIs could be triggered in different ways
  • For example you could have a simple workflow that flips a variable between true/false and expose it as a context then whenever that changes to true, trigger the API
  • lucasg-1368440930444509274

    Lucas G

    2 months ago

    You could move the APIs up to a provider wrapper as well and have workflows to trigger them exposed as contexts
  • There's a lot of ways to set it up. It all depends on your app's structure
  • gerson_ca-1368443175471153225

    Gerson CA

    2 months ago

    Ooh I see I like the variable idea. Thanks @Lucas G I appreciate the explanation and help 🙌
  • lucasg-1368444267927703603

    Lucas G

    2 months ago

    I personally would go with a provider component that has no divs, just a slot and it houses your main APIs and have it expose the data to the app
  • It works pretty nicely with SPAs