Leptos Query
About
Leptos Query is a robust asynchronous state management library for Leptos, providing simplified data fetching, integrated reactivity, server-side rendering support, and intelligent cache management.
Heavily inspired by Tanstack Query.
Read the introduction article here: The Forging of Leptos Query
Why Choose Leptos Query?
Leptos Query focuses on simplifying your data fetching process and keeping your application's state effortlessly synchronized and up-to-date. Here's how it's done:
Key Features
-
Configurable Caching & SWR: Queries are cached by default, ensuring quick access to your data. You can configure your stale and cache times per query with Stale While Revalidate (SWR) system.
-
Reactivity at the Core: Leptos Query deeply integrates with Leptos' reactive system to transform asynchronous query fetchers into reactive Signals.
-
Server-Side Rendering (SSR) Compatibility: Fetch your queries on the server and smoothly serialize them to the client, just as you would with a Leptos Resource.
-
Efficient De-duplication: No unnecessary fetches here! If you make multiple queries with the same Key, Leptos Query smartly fetches only once.
-
Manual Invalidation: Control when your queries should be invalidated and refetched for that ultimate flexibility.
-
Scheduled Refetching: Set up your queries to refetch on a customized schedule, keeping your data fresh as per your needs.
-
Manual Query Data Mutations: Useful when you have updated a value and you want to manually set it in cache instead of waiting for query to refetch.
Version compatibility for Leptos and Leptos Query
The table below shows the compatible versions of leptos_query for each leptos version. Ensure you are using compatible versions to avoid potential issues.
leptos version |
leptos_query version |
|---|---|
| 0.6.* | 0.4.* |
| 0.5.* | 0.3.* |
Installation
Then add the relevant feature(s) to your Cargo.toml
[]
= [
"leptos_query/hydrate",
# ...
]
= [
"leptos_query/ssr",
# ...
]
Quick Start
If you are using SSR you may have to use
supress_query_loadin your server's main function. See the FAQ for more information.
In the root of your App, provide a query client:
use *;
use *;
Then make a query function.
use *;
use *;
use Duration;
use *;
// Data type.
// Monkey fetcher.
async
// Query for a Monkey.
Now you can use the query in any component in your app.
For a complete working example see the example directory