Quantcast
Channel: Active questions tagged svelte - Stack Overflow
Viewing all articles
Browse latest Browse all 1662

Consume REST API and use response data inside multiple components (Not page)

$
0
0

I have a page with multiple components, like tables and displayed in one page.

My +page.svelte:

<script>..<script><div class="page-content"><Row><Table1 /><List /> --><Table2 /></Row></div>

My REST API call as an example in one of my component:

<script>let todos // 'forever' undefined    async function fetchTodos() {               const response = await fetch('https://jsonplaceholder.typicode.com/todos/')        return await response.json()    }</script>{#await fetchTodos() then todos}<h1>{todos.length} todos!</h1>{/await}

However if I want to use it inside each tables and some of the data in the List component, according to this solution I need to place the same call all of my component and I generate unnecessary extra same calls.

  1. What is the proper way to call the endpoint once and reuse the response data in all components?
  2. I want to poll the REST API call in every 15 seconds and refresh the fields inside the component, so saving extra call would be crucial. Any polling solution?

Viewing all articles
Browse latest Browse all 1662

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>