Query
A popular pattern popularised by react-query and swr is to use a stale-while-revalidate pattern. Impact enables this pattern with its query
primitive.
ts
import { query } from "@impact-react/signals";
import { createApi } from "./api";
function AppStore() {
const api = createApi();
const [itemsQuery, invalidateItems] = query(() => api.getItems());
return {
itemsQuery,
invalidateItems,
};
}
The itemsQuery
is a signal of the promise and state of the query:
ts
const appStore = useAppStore();
const items = appStore.itemsQuery();
items.promise; // The observable promise
item.state; // idle, fetching or refetching