# Leptos Query
[](https://github.com/gaucho-labs/leptos-query) [](https://crates.io/crates/leptos_query) [](https://docs.rs/leptos_query) [](https://github.com/gaucho-labs/leptos_query/actions?query=branch%3Amain) [FAQ](https://github.com/gaucho-labs/leptos-query/blob/main/FAQ.md) | [Examples](https://github.com/gaucho-labs/leptos-query/tree/main/example/) | [Live Demo](https://leptos-query-demo.vercel.app/) ## About Leptos Query is a async state management library for [Leptos](https://github.com/leptos-rs/leptos). Heavily inspired by [Tanstack Query](https://tanstack.com/query/latest/). 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 encapsulates `use_query` and other methods for managing queries. - [`use_query`][crate::use_query::use_query()] - A query primitive for reading, caching, and refetching data. ## Feature Flags - `csr` Client-side rendering: Use queries on the client. - `ssr` Server-side rendering: Initiate queries on the server. - `hydrate` Hydration: 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 ```bash cargo add leptos_query ``` Then add the relevant feature(s) to your `Cargo.toml` ```toml [features] hydrate = [ "leptos_query/hydrate", # ... ] ssr = [ "leptos_query/ssr", # ... ] ``` ## Quick Start > If you are using SSR you may have to use [`supress_query_load`](https://docs.rs/leptos_query/latest/leptos_query/fn.suppress_query_load.html) in your server's main function. See the [FAQ](https://github.com/gaucho-labs/leptos_query/blob/main/FAQ.md#why-am-i-getting-a-panic-on-my-leptos-main-function) 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. ```rust use leptos_query::*; use leptos::*; #[component] pub fn App() -> impl IntoView { // Provides Query Client for entire app. provide_query_client(); // Rest of App... } ``` Then make a query function with [`create_query`][crate::create_query::create_query()] ```rust use leptos::*; use leptos_query::*; // Query for a track. fn track_query() -> QueryScope