readme.rs 897 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. use std::time::Duration;
  2. use dioxus::prelude::*;
  3. fn main() {
  4. launch(app);
  5. }
  6. fn app() -> Element {
  7. let mut count = use_signal(|| 0);
  8. use_effect(move || {
  9. println!("The count is now: {}", count);
  10. });
  11. rsx! {
  12. h1 { "High-Five counter: {count}" }
  13. Child { sig: count }
  14. button { onclick: move |_| count += 1, "Up high!" }
  15. button { onclick: move |_| count -= 1, "Down low!" }
  16. }
  17. }
  18. #[component]
  19. fn Child(sig: Signal<i32>) -> Element {
  20. let doubled = use_memo(move || sig() * 2);
  21. let tripled = use_async_memo(move || async move {
  22. tokio::time::sleep(Duration::from_millis(200)).await;
  23. sig() * 3
  24. });
  25. let trippled = use_memo(move || match tripled.value() {
  26. Some(v) => v.cloned(),
  27. None => 1338,
  28. });
  29. rsx! {
  30. "The count is: {sig}, doubled: {doubled}, tripled: {trippled}"
  31. }
  32. }