signals.rs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. use dioxus::prelude::*;
  2. use std::time::Duration;
  3. fn main() {
  4. launch_desktop(app);
  5. }
  6. fn app() -> Element {
  7. let mut running = use_signal(|| true);
  8. let mut count = use_signal(|| 0);
  9. let mut saved_values = use_signal(|| vec![0.to_string()]);
  10. // use_memo will recompute the value of the signal whenever the captured signals change
  11. let doubled_count = use_memo(move || count() * 2);
  12. // use_effect will subscribe to any changes in the signal values it captures
  13. // effects will always run after first mount and then whenever the signal values change
  14. use_effect(move || println!("Count changed to {count}"));
  15. // We can do early returns and conditional rendering which will pause all futures that haven't been polled
  16. if count() > 30 {
  17. return rsx! {
  18. h1 { "Count is too high!" }
  19. button { onclick: move |_| count.set(0), "Press to reset" }
  20. };
  21. }
  22. // use_future will spawn an infinitely running future that can be started and stopped
  23. use_future(move || async move {
  24. loop {
  25. if running() {
  26. count += 1;
  27. }
  28. tokio::time::sleep(Duration::from_millis(400)).await;
  29. }
  30. });
  31. // use_resource will spawn a future that resolves to a value
  32. let _slow_count = use_resource(move || async move {
  33. tokio::time::sleep(Duration::from_millis(200)).await;
  34. count() * 2
  35. });
  36. rsx! {
  37. h1 { "High-Five counter: {count}" }
  38. button { onclick: move |_| count += 1, "Up high!" }
  39. button { onclick: move |_| count -= 1, "Down low!" }
  40. button { onclick: move |_| running.toggle(), "Toggle counter" }
  41. button { onclick: move |_| saved_values.push(count.to_string()), "Save this value" }
  42. button { onclick: move |_| saved_values.clear(), "Clear saved values" }
  43. // We can do boolean operations on the current signal value
  44. if count() > 5 {
  45. h2 { "High five!" }
  46. }
  47. // We can cleanly map signals with iterators
  48. for value in saved_values.iter() {
  49. h3 { "Saved value: {value}" }
  50. }
  51. // We can also use the signal value as a slice
  52. if let [ref first, .., ref last] = saved_values.read().as_slice() {
  53. li { "First and last: {first}, {last}" }
  54. } else {
  55. "No saved values"
  56. }
  57. // You can pass a value directly to any prop that accepts a signal
  58. Child { count: doubled_count() }
  59. Child { count: doubled_count }
  60. }
  61. }
  62. #[component]
  63. fn Child(mut count: ReadOnlySignal<i32>) -> Element {
  64. println!("rendering child with count {count}");
  65. rsx! {
  66. h1 { "{count}" }
  67. }
  68. }