rsx_compile_fail.rs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //! This example just flexes the ability to use arbitrary expressions within RSX.
  2. //! It also proves that lifetimes work properly, especially when used with use_ref
  3. use dioxus::prelude::*;
  4. fn main() {
  5. let mut vdom = VirtualDom::new(example);
  6. vdom.rebuild();
  7. let out = dioxus_ssr::render_vdom_cfg(&vdom, |c| c.newline(true).indent(true));
  8. println!("{}", out);
  9. }
  10. fn example(cx: Scope) -> Element {
  11. let items = use_state(&cx, || {
  12. vec![Thing {
  13. a: "asd".to_string(),
  14. b: 10,
  15. }]
  16. });
  17. let things = use_ref(&cx, || {
  18. vec![Thing {
  19. a: "asd".to_string(),
  20. b: 10,
  21. }]
  22. });
  23. let things_list = things.read();
  24. let mything = use_ref(&cx, || Some(String::from("asd")));
  25. let mything_read = mything.read();
  26. cx.render(rsx!(
  27. div {
  28. div { id: "asd",
  29. "your neighborhood spiderman"
  30. items.iter().cycle().take(5).map(|f| rsx!{
  31. div { "{f.a}" }
  32. })
  33. things_list.iter().map(|f| rsx!{
  34. div { "{f.a}" "{f.b}" }
  35. })
  36. mything_read.as_ref().map(|f| rsx! {
  37. div { "{f}" }
  38. })
  39. }
  40. }
  41. ))
  42. }
  43. struct Thing {
  44. a: String,
  45. b: u32,
  46. }