#dioxus #sync #state-management #async #mutation

dioxus-query

Fully-typed, async, reusable cached state management for Dioxus 🧬

12 releases (6 breaking)

0.7.0 Apr 5, 2025
0.6.1 Mar 1, 2025
0.6.0 Dec 10, 2024
0.5.1 Jun 23, 2024
0.3.2 Sep 10, 2023

#1365 in GUI

Download history 41/week @ 2025-01-01 31/week @ 2025-01-08 38/week @ 2025-01-15 26/week @ 2025-01-22 66/week @ 2025-01-29 229/week @ 2025-02-05 90/week @ 2025-02-12 45/week @ 2025-02-19 248/week @ 2025-02-26 119/week @ 2025-03-05 68/week @ 2025-03-12 134/week @ 2025-03-19 76/week @ 2025-03-26 319/week @ 2025-04-02 139/week @ 2025-04-09 61/week @ 2025-04-16

606 downloads per month

MIT license

43KB
644 lines

Discord Server

dioxus-query 🦀⚡

Fully-typed, async, reusable cached state management for Dioxus 🧬. Inspired by TanStack Query.

See the Docs or join the Discord.

Support

  • Dioxus v0.6 🧬
  • All renderers (web, desktop, freya, etc)
  • Both WASM and native targets

Installation

Install the latest release:

cargo add dioxus-query

Example

cargo run --example simple

Usage

#[derive(Clone, PartialEq, Eq, Hash)]
enum QueryKey {
    User(usize),
}

#[derive(Debug)]
enum QueryError {
    UserNotFound(usize),
    Unknown
}

#[derive(PartialEq, Debug)]
enum QueryValue {
    UserName(String),
}

async fn fetch_user(keys: Vec<QueryKey>) -> QueryResult<QueryValue, QueryError> {
    if let Some(QueryKey::User(id)) = keys.first() {
        println!("Fetching user {id}");
        sleep(Duration::from_millis(1000)).await;
        match id {
            0 => Ok(QueryValue::UserName("Marc".to_string())),
            _ => Err(QueryError::UserNotFound(*id)),
        }
    } else {
        Err(QueryError::Unknown)
    }
}

#[allow(non_snake_case)]
#[component]
fn User(id: usize) -> Element {
   let value = use_get_query([QueryKey::User(id)], fetch_user);

    rsx!( p { "{value.result().value():?}" } )
}

fn app() -> Element {
    let client = use_init_query_client::<QueryValue, QueryError, QueryKey>();

    let onclick = move |_| {
         client.invalidate_queries(&[QueryKey::User(0)]);
    };

    rsx!(
        User { id: 0 }
        button { onclick, label { "Refresh" } }
    )
}

Features

  • Renderer-agnostic
  • Queries and mutations
  • Typed Mutations, Query keys, Errors and Values
  • Invalidate queries manually
  • Invalidate queries when keys change
  • Concurrent and batching of queries
  • Concurrent mutations
  • Background interval invalidation
  • On window focus invalidation

To Do

  • Tests
  • Documentation
  • Real-world examples

MIT License

Dependencies

~4–11MB
~110K SLoC