Leptos Query
About
Leptos Query is a async state management library for Leptos.
Heavily inspired by Tanstack Query.
Queries are useful for data fetching, caching, and synchronization with server state.
A Query provides:
- Caching
- Request de-duplication
- Invalidation
- Background refetching
- Refetch intervals
- Memory management with cache lifetimes
- Cancellation
- Debugging tools
- Optimistic updates
- Client side cache persistance (localstorage, indexdb, custom, etc.)
The main entry points to using Queries are:
create_query- Recommended: Creates a [QueryScope] which encapsulatesuse_queryand other methods for managing queries.- [
use_query][crate::use_query::use_query()] - A query primitive for reading, caching, and refetching data.
Feature Flags
csrClient-side rendering: Use queries on the client.ssrServer-side rendering: Initiate queries on the server.hydrateHydration: Ensure that queries are hydrated on the client, when using server-side rendering.local_storage- Enables local storage persistance for queries.index_db- Enables index db persistance for queries.
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.5.* or 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 with [provide_query_client] or [provide_query_client_with_options] if you want to override the default options.
use *;
use *;
Then make a query function with [create_query][crate::create_query::create_query()]
use *;
use *;
// Query for a track.
// Make a key type.
;
// The result of the query fetcher.
// Query fetcher.
async
Now you can use the query in any component in your app.
use *;
use *;
For a complete working example see the example directory
Devtools Quickstart
To use the devtools, you need to add the devtools crate:
Then in your cargo.toml enable the csr feature.
Hydrate Example
- If your app is using SSR, then this should go under the "hydrate" feature.
[]
= [
"leptos_query_devtools/csr",
]
CSR Example
- If your app is using CSR, then this should go under the "csr" feature.
[]
= [
"leptos_query_devtools/csr",
]
Then in your app, render the devtools component. Make sure you also provide the query client.
Devtools will by default only show in development mode. It will not be shown, or included in binary, when you build your app in release mode. If you want to override this behaviour, you can enable the force feature.
use LeptosQueryDevtools;
use *;
use *;