skeleton of use_server_future with reactivity

This commit is contained in:
Jonathan Kelley committed 2024-02-01 13:21:52 -08:00
1 parent b3ed337b6b
commit 0c71b95e82
11 files changed
+200 -193

No files matched your search

+2 -2
View File
@@ -7,7 +7,7 @@ fn main() {
fn app() -> Element {
let mut breed = use_signal(|| "deerhound".to_string());
let breed_list = use_resource(move || async move {
let breed_list = use_async_memo(move || async move {
let list = reqwest::get("https://dog.ceo/api/breeds/list/all")
.await
.unwrap()
@@ -44,7 +44,7 @@ fn app() -> Element {
#[component]
fn BreedPic(breed: Signal<String>) -> Element {
let fut = use_resource(move || async move {
let fut = use_async_memo(move || async move {
reqwest::get(format!("https://dog.ceo/api/breed/{breed}/images/random"))
.await
.unwrap()
+1 -1
View File
@@ -5,7 +5,7 @@ fn main() {
}
fn app() -> Element {
let future = use_resource(move || async move {
let future = use_async_memo(move || async move {
let mut eval = eval(
r#"
dioxus.send("Hi from JS!");
+59
View File
@@ -0,0 +1,59 @@
use dioxus::prelude::*;
fn main() {
launch(app);
}
fn app() -> Element {
let val = use_server_future(fetch_users).suspend()?;
rsx! {
h1 { "Users" }
}
}
#[component]
fn ClientComponent(name: Signal<i32>, id: i64) -> Element {
rsx! {
div { "Name: {name}, ID: {id}" }
button {
onclick: move |_| async move {
// Optimistically change the name on the client
name.set("new name".to_string());
// Change the name on the server
change_name(id, "new name".to_string()).await;
// And then re-fetch the user list
revalidate(user_list);
},
"Change name"
}
}
}
#[derive(Table)]
struct Users {
name: String,
age: i32,
}
#[server]
async fn fetch_users() -> Result<Element> {
let users = get_users().await?;
Ok(rsx! {
for user in users {
ClientComponent {
name: user.name,
id: user.id,
}
}
})
}
#[server]
async fn change_name(id: i64, new_name: String) -> Result<()> {
// Send a request to the server to change the name
}
+1 -1
View File
@@ -39,7 +39,7 @@ fn app() -> Element {
});
// use_resource will spawn a future that resolves to a value - essentially an async memo
let _slow_count = use_resource(move || async move {
let _slow_count = use_async_memo(move || async move {
tokio::time::sleep(Duration::from_millis(200)).await;
count() * 2
});