rsx_syntax.rs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. use std::future::IntoFuture;
  2. use dioxus::prelude::*;
  3. fn basic_syntax_is_a_template(cx: Scope) -> Element {
  4. let asd = 123;
  5. let var = 123;
  6. cx.render(rsx! {
  7. div { key: "12345",
  8. class: "asd",
  9. class: "{asd}",
  10. onclick: move |_| {},
  11. div { "{var}" }
  12. div {
  13. h1 { "var" }
  14. p { "you're great!" }
  15. div { background_color: "red",
  16. h1 { "var" }
  17. div { b { "asd" } "not great" }
  18. }
  19. p { "you're great!" }
  20. }
  21. }
  22. })
  23. }
  24. #[inline_props]
  25. fn suspense_boundary<'a>(cx: Scope<'a>, children: Element<'a>) -> Element {
  26. cx.use_hook(|| cx.provide_context(SuspenseBoundary::new(cx.scope_id())));
  27. cx.render(rsx! { children })
  28. }
  29. fn basic_child(cx: Scope) -> Element {
  30. cx.render(rsx! {
  31. div { "basic child 1" }
  32. })
  33. }
  34. async fn async_child(cx: Scope<'_>) -> Element {
  35. let username = use_future!(cx, || async {
  36. tokio::time::sleep(std::time::Duration::from_secs(1)).await;
  37. "async child 1"
  38. });
  39. let age = use_future!(cx, || async {
  40. tokio::time::sleep(std::time::Duration::from_secs(2)).await;
  41. println!("long future completed");
  42. 1234
  43. });
  44. let (_user, _age) = use_future!(cx, || async {
  45. tokio::join!(
  46. tokio::time::sleep(std::time::Duration::from_secs(1)),
  47. tokio::time::sleep(std::time::Duration::from_secs(2))
  48. );
  49. ("async child 1", 1234)
  50. })
  51. .await;
  52. let (username, age) = tokio::join!(username.into_future(), age.into_future());
  53. cx.render(rsx!(
  54. div { "Hello! {username}, you are {age}, {_user} {_age}" }
  55. ))
  56. }
  57. #[tokio::test]
  58. async fn basic_prints() {
  59. let mut dom = VirtualDom::new(|cx| {
  60. cx.render(rsx! {
  61. div {
  62. h1 { "var" }
  63. suspense_boundary {
  64. basic_child { }
  65. async_child { }
  66. }
  67. }
  68. })
  69. });
  70. dbg!(dom.rebuild());
  71. dom.wait_for_work().await;
  72. dbg!(dom.rebuild());
  73. }