diff --git a/examples/memo_chain.rs b/examples/memo_chain.rs new file mode 100644 index 000000000..3d9c8ae30 --- /dev/null +++ b/examples/memo_chain.rs @@ -0,0 +1,52 @@ +use dioxus::prelude::*; + +fn main() { + launch_desktop(app); +} + +fn app() -> Element { + let mut state = use_signal(|| 0); + let mut depth = use_signal(|| 1 as usize); + let mut items = use_memo(move || (0..depth()).map(|f| f as _).collect::>()); + + let a = use_memo(move || state() + 1); + + println!("rendering app"); + + rsx! { + button { onclick: move |_| state += 1, "Increment" } + button { onclick: move |_| depth += 1, "Add depth" } + button { onclick: move |_| depth -= 1, "Remove depth" } + Child { + depth: depth.into(), + items: items, + state: a, + } + } +} + +#[component] +fn Child( + state: ReadOnlySignal, + items: ReadOnlySignal>, + depth: ReadOnlySignal, +) -> Element { + if depth() == 0 { + return None; + } + + println!("rendering child: {}", depth()); + + let state = use_memo(move || state() + 1); + let item = use_memo(move || items()[dbg!(depth()) - 1]); + let depth = use_memo(move || depth() - 1); + + rsx! { + h3 { "Depth({depth})-Item({item}): {state}"} + Child { + depth, + state, + items + } + } +} diff --git a/examples/signals.rs b/examples/signals.rs index 43a9902d4..b7de86146 100644 --- a/examples/signals.rs +++ b/examples/signals.rs @@ -39,7 +39,7 @@ fn app() -> Element { button { onclick: move |_| count -= 1, "Down low!" } button { onclick: move |_| running.toggle(), "Toggle counter" } button { onclick: move |_| saved_values.push(count().to_string()), "Save this value" } - button { onclick: move |_| saved_values.write().clear(), "Clear saved values" } + button { onclick: move |_| saved_values.clear(), "Clear saved values" } // We can do boolean operations on the current signal value if count() > 5 { @@ -47,7 +47,7 @@ fn app() -> Element { } // We can cleanly map signals with iterators - for value in saved_values.read().iter() { + for value in saved_values.iter() { h3 { "Saved value: {value}" } } diff --git a/packages/fullstack/src/config.rs b/packages/fullstack/src/config.rs index b0db4e718..68347b7d6 100644 --- a/packages/fullstack/src/config.rs +++ b/packages/fullstack/src/config.rs @@ -1,7 +1,7 @@ //! Launch helper macros for fullstack apps #![allow(unused)] use crate::prelude::*; -use dioxus_lib::prelude::{dioxus_core::AnyProps, *}; +use dioxus_lib::prelude::*; use std::sync::Arc; /// Settings for a fullstack app.