Implement effects using reactivecontext

This commit is contained in:
Jonathan Kelley committed 2024-02-01 13:21:52 -08:00
1 parent 968f24a7b3
commit 7c2947a131
21 files changed
+287 -320

No files matched your search

+1 -1
View File
@@ -22,7 +22,7 @@ fn app() -> Element {
res
});
match future.value().read().as_ref() {
match future.value().as_ref() {
Some(v) => rsx!( p { "{v}" } ),
_ => rsx!( p { "waiting.." } ),
}
+15
View File
@@ -7,8 +7,23 @@ fn main() {
fn app() -> Element {
let mut count = use_signal(|| 0);
use_effect(move || {
println!("The count is now: {}", count);
});
rsx! {
h1 { "High-Five counter: {count}" }
Child { sig: count }
button { onclick: move |_| count += 1, "Up high!" }
button { onclick: move |_| count -= 1, "Down low!" }
}
}
#[component]
fn Child(sig: Signal<i32>) -> Element {
let doubled = use_memo(move || sig() * 2);
rsx! {
"The count is: {sig}, doubled: {doubled}"
}
}
-59
View File
@@ -1,59 +0,0 @@
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
}
+2
View File
@@ -77,6 +77,8 @@ fn app() -> Element {
#[component]
fn Child(mut count: ReadOnlySignal<i32>) -> Element {
println!("rendering child with count {count}");
rsx! {
h1 { "{count}" }
}