#sync #dioxus #async #state #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

#1035 in GUI

Download history 28/week @ 2025-01-20 43/week @ 2025-01-27 235/week @ 2025-02-03 93/week @ 2025-02-10 52/week @ 2025-02-17 176/week @ 2025-02-24 165/week @ 2025-03-03 82/week @ 2025-03-10 136/week @ 2025-03-17 76/week @ 2025-03-24 271/week @ 2025-03-31 169/week @ 2025-04-07 87/week @ 2025-04-14 44/week @ 2025-04-21 32/week @ 2025-04-28 35/week @ 2025-05-05

207 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
~111K SLoC