mirror of
https://github.com/DioxusLabs/dioxus
synced 2024-11-10 06:34:20 +00:00
20d146d9bd
* improve documentation for the fullstack server context * Add a section about axum integration to the crate root docs * make serve_dioxus_application accept the cfg builder directly * remove unused server_fn module * improve fullstack config docs * improve documentation for the server function macro * fix axum router extension link * Fix doc tests * Fix launch builder * Simplify the launch builder * don't re-export launch in the prelude * refactor fullstack launch * Fix fullstack launch builder * Update static generation with the new builder api * fix some formatting/overly broad launch replacements * fix custom menu example * fix fullstack/static generation examples * Fix static generation launch * A few small formatting fixes * Fix a few doc tests * implement LaunchConfig for serve configs * fix fullstack launch with separate web and server launch methods * fix check with all features * dont expose inner core module * clippy and check * fix readme --------- Co-authored-by: Jonathan Kelley <jkelleyrtp@gmail.com>
65 lines
1.7 KiB
Rust
65 lines
1.7 KiB
Rust
//! Backgrounded futures example
|
|
//!
|
|
//! This showcases how use_future, use_memo, and use_effect will stop running if the component returns early.
|
|
//! Generally you should avoid using early returns around hooks since most hooks are not properly designed to
|
|
//! handle early returns. However, use_future *does* pause the future when the component returns early, and so
|
|
//! hooks that build on top of it like use_memo and use_effect will also pause.
|
|
//!
|
|
//! This example is more of a demonstration of the behavior than a practical use case, but it's still interesting to see.
|
|
|
|
use async_std::task::sleep;
|
|
use dioxus::prelude::*;
|
|
|
|
fn main() {
|
|
dioxus::launch(app);
|
|
}
|
|
|
|
fn app() -> Element {
|
|
let mut show_child = use_signal(|| true);
|
|
let mut count = use_signal(|| 0);
|
|
|
|
let child = use_memo(move || {
|
|
rsx! {
|
|
Child { count }
|
|
}
|
|
});
|
|
|
|
rsx! {
|
|
// Some toggle/controls to show the child or increment the count
|
|
button { onclick: move |_| show_child.toggle(), "Toggle child" }
|
|
button { onclick: move |_| count += 1, "Increment count" }
|
|
|
|
if show_child() {
|
|
{child()}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[component]
|
|
fn Child(count: Signal<i32>) -> Element {
|
|
let mut early_return = use_signal(|| false);
|
|
|
|
let early = rsx! {
|
|
button { onclick: move |_| early_return.toggle(), "Toggle {early_return} early return" }
|
|
};
|
|
|
|
if early_return() {
|
|
return early;
|
|
}
|
|
|
|
use_future(move || async move {
|
|
loop {
|
|
sleep(std::time::Duration::from_millis(100)).await;
|
|
println!("Child")
|
|
}
|
|
});
|
|
|
|
use_effect(move || println!("Child count: {}", count()));
|
|
|
|
rsx! {
|
|
div {
|
|
"Child component"
|
|
{early}
|
|
}
|
|
}
|
|
}
|