signals.rs 3.1 KB

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