Skip to content

zhangzhonglai/dioxus-query

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Rust 100.0%