#dioxus #state #synchronization #async #cache

dioxus-query

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

10 releases (5 breaking)

new 0.6.0 Dec 10, 2024
0.5.1 Jun 23, 2024
0.4.0 Dec 16, 2023
0.3.2 Sep 10, 2023
0.1.1 Aug 5, 2023

#678 in GUI

Download history 74/week @ 2024-08-13 18/week @ 2024-08-20 47/week @ 2024-08-27 55/week @ 2024-09-03 108/week @ 2024-09-10 44/week @ 2024-09-17 111/week @ 2024-09-24 88/week @ 2024-10-01 80/week @ 2024-10-08 43/week @ 2024-10-15 6/week @ 2024-10-22 10/week @ 2024-10-29 26/week @ 2024-11-05 31/week @ 2024-11-12 80/week @ 2024-11-19 21/week @ 2024-11-26

164 downloads per month

MIT license

41KB
642 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.

⚠️ Work in progress ⚠️

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)),
        }
        .into()
    } else {
        QueryResult::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 {
    use_init_query_client::<QueryValue, QueryError, QueryKey>();
    let client = use_query_client::<QueryValue, QueryError, QueryKey>();

    let onclick = move |_| {
         client.invalidate_query(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

To Do

  • Tests
  • Documentation
  • Real-world examples

MIT License

Dependencies

~4–10MB
~112K SLoC