hot_reload.rs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 + 10);
  23. },
  24. p{
  25. "High-Five counter: {count}",
  26. }
  27. Comp{
  28. color: "#083289"
  29. }
  30. Comp{
  31. color: "green"
  32. }
  33. }
  34. })
  35. }
  36. #[derive(PartialEq, Props)]
  37. struct CompProps {
  38. color: &'static str,
  39. }
  40. fn Comp(cx: Scope<CompProps>) -> Element {
  41. cx.render(rsx! {
  42. h1 {
  43. color: cx.props.color,
  44. "Hello, from a component!"
  45. }
  46. })
  47. }