hot_reload.rs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. use dioxus::prelude::*;
  2. use std::time::Duration;
  3. fn main() {
  4. dioxus::desktop::launch_with_props(with_hot_reload, app, |b| b);
  5. }
  6. fn app(cx: Scope) -> Element {
  7. let count = use_state(&cx, || 170);
  8. use_future(&cx, (), move |_| {
  9. let mut count = count.clone();
  10. async move {
  11. loop {
  12. tokio::time::sleep(Duration::from_millis(1000)).await;
  13. count += 1;
  14. }
  15. }
  16. });
  17. cx.render(rsx! {
  18. div {
  19. width: format!("{}px", count),
  20. background_color: "#999999",
  21. onclick: move |_| {
  22. count.modify(|count| *count + 1);
  23. },
  24. "High-Five counter: {count}",
  25. Comp{
  26. color: "#083289"
  27. }
  28. Comp{
  29. color: "green"
  30. }
  31. }
  32. })
  33. }
  34. #[derive(PartialEq, Props)]
  35. struct CompProps {
  36. color: &'static str,
  37. }
  38. fn Comp(cx: Scope<CompProps>) -> Element {
  39. cx.render(rsx! {
  40. h1 {
  41. color: cx.props.color,
  42. "Hello, from a component!"
  43. }
  44. })
  45. }