Leptos Fetch
About
Leptos Fetch is a async state management library for Leptos.
The successor of, and heavily inspired by Leptos Query, which has been unmaintained for ~1 year.
Queries are useful for data fetching, caching, and synchronization with server state.
This crate provides:
- Caching
- Request de-duplication
- Invalidation
- Background refetching
Refetch intervals- Memory management with cache lifetimes
CancellationDebugging tools- Optimistic updates
Client side cache persistance (localstorage, indexdb, custom, etc.)
Lines that have a strike through are features not currently brought over from Leptos Query.
Feature Flags
ssrServer-side rendering: Initiate queries on the server.
Version compatibility for Leptos and Leptos Fetch
The table below shows the compatible versions of leptos-fetch for each leptos version. Ensure you are using compatible versions to avoid potential issues.
leptos version |
leptos-fetch version |
|---|---|
| 0.7.* | 0.1.* 0.2.* |
Installation
If using ssr, add the relevant feature to your Cargo.toml when in ssr:
[]
= [
"leptos-fetch/ssr",
# ...
]
Quick Start
In the root of your App, provide a query client with [QueryClient::provide()] or [QueryClient::provide_with_options()] if you want to override the default options.
use *;
use QueryClient;
Any async function can be used as a query:
/// The query function.
async
Now you can use the query in any component in your app.
use *;
use QueryClient;
/// The query function.
async
You can read more about leptos resources in the Leptos Book
QueryScope and QueryScopeLocal can be used instead of directly passing a function to [QueryClient] methods to only apply to one query type.
These [QueryOptions] will be combined with the global [QueryOptions] set on the [crate::QueryClient], with the local options taking precedence.
use Duration;
use ;
// this can be used just like the function directly in QueryClient methods.
/// The query function.
async
The [QueryClient] contains many documented utility methods other than resources for:
- Declaratively fetching queries
- Declaratively prefetching queries
- Mutating cached queries
- Invalidating cached queries
- Accessing cached queries