Crates.io | dioxus-query |
lib.rs | dioxus-query |
version | 0.5.1 |
source | src |
created_at | 2023-08-05 20:26:40.674356 |
updated_at | 2024-06-23 11:05:48.811896 |
description | Fully-typed, async, reusable cached state management for Dioxus 🧬 |
homepage | https://github.com/marc2332/dioxus-query |
repository | https://github.com/marc2332/dioxus-query |
max_upload_size | |
id | 936774 |
size | 145,937 |
Fully-typed, async, reusable cached state management for Dioxus 🧬. Inspired by TanStack Query
.
See the Docs or join the Discord.
⚠️ Work in progress ⚠️
Install the latest release:
cargo add dioxus-query
cargo run --example simple
#[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" } }
)
}
MIT License