1
0

stale_memo.rs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. use dioxus::prelude::*;
  2. fn main() {
  3. launch_desktop(app);
  4. }
  5. fn app() -> Element {
  6. let mut state = use_signal(|| 0);
  7. let mut depth = use_signal(|| 1_usize);
  8. if depth() == 5 {
  9. return rsx! {
  10. div { "Max depth reached" }
  11. button { onclick: move |_| depth -= 1, "Remove depth" }
  12. };
  13. }
  14. let items = use_memo(move || (0..depth()).map(|f| f as _).collect::<Vec<isize>>());
  15. rsx! {
  16. button { onclick: move |_| state += 1, "Increment" }
  17. button { onclick: move |_| depth += 1, "Add depth" }
  18. button {
  19. onclick: move |_| async move {
  20. depth += 1;
  21. tokio::time::sleep(std::time::Duration::from_millis(100)).await;
  22. dbg!(items.read());
  23. // if depth() is 5, this will be the old since the memo hasn't been re-computed
  24. // use_memos are only re-computed when the signals they capture change
  25. // *and* they are used in the current render
  26. // If the use_memo isn't used, it can't be re-computed!
  27. },
  28. "Add depth with sleep"
  29. }
  30. }
  31. }