#dioxus #synchronization #state #async

dioxus-query

Fully-typed, async, reusable state management and synchronization for Dioxus 🧬

7 unstable releases (3 breaking)

0.4.0 Dec 16, 2023
0.3.2 Sep 10, 2023
0.3.1 Aug 19, 2023
0.2.0 Aug 18, 2023
0.1.1 Aug 5, 2023

#315 in GUI

Download history 18/week @ 2024-02-19 11/week @ 2024-02-26 25/week @ 2024-03-11 41/week @ 2024-04-01

66 downloads per month

MIT license

30KB
543 lines

Discord Server

dioxus-query 🦀⚡

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

See the Docs or join the Discord.

⚠️ Work in progress ⚠️

Support

  • Dioxus v0.4 🧬
  • 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 QueryKeys {
    User(usize),
}

#[derive(Clone, PartialEq, Eq, Hash, Debug)]
enum QueryError {
    UserNotFound(usize),
    Unknown
}

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

async fn fetch_user(keys: Vec<QueryKeys>) -> QueryResult<QueryValue, QueryError> {
    if let Some(QueryKeys::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)]
#[inline_props]
fn User(cx: Scope, id: usize) -> Element {
   let value = use_query(cx, || vec![QueryKeys::User(*id)], fetch_user);

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

fn app(cx: Scope) -> Element {
     use_init_query_client::<QueryValue, QueryError, QueryKeys>(cx);
    let client = use_query_client::<QueryValue, QueryError, QueryKeys>(cx);

    let refresh = |_| {
         client.invalidate_query(QueryKeys::User(0));
    };

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

Features

  • Renderer-agnostic
  • Typed Query keys, errors and results
  • Manual query/queries invalidation
  • Automatic/smart query invalidation
  • Query aborting
  • Global Query + Function caching
  • Concurrent queries and mutations

To Do

  • Tests
  • Documentation
  • Real-world examples
  • Clean up code

MIT License

Dependencies

~2.2–4MB
~79K SLoC