Resource
A common activity in web apps is fetching information from services. VistaJS combines data fetching with reactivity to leverage the best of both worlds
Fetching data
import { signal, resource } from 'vistajs/core';
import { Component, html } from 'vistajs/dom';
import { boostrap } from 'vistajs/boot';
const home = Component('app-home', () => {
const products = resource({
loader: async () => {
const response = await fetch('https://dummyjson.com/products?limit=3');
const data = await response.json();
return data?.products.map((product: any) => product.title);
},
});
return () => html`
<ul>
${products.value()?.map((product: string) => {
return `<li>${product}</li>`;
})}
</ul>
`;
});
const root = document.querySelector('#app') as Element;
boostrap(root, home);Reactive data fetching
You can control when the fetch is triggered by combining signals and resources, passing a parameter to the resource function.
States
Leveraging signals, resource allows you to react to loading states and errors
Loading
Error
Last updated