shorthand.rs 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. //! Dioxus supports shorthand syntax for creating elements and components.
  2. use dioxus::prelude::*;
  3. fn main() {
  4. dioxus::launch(app);
  5. }
  6. fn app() -> Element {
  7. let a = 123;
  8. let b = 456;
  9. let c = 789;
  10. let class = "class";
  11. let id = "id";
  12. // todo: i'd like it for children on elements to be inferred as the children of the element
  13. // also should shorthands understand references/dereferences?
  14. // ie **a, *a, &a, &mut a, etc
  15. let children = rsx! { "Child" };
  16. let onclick = move |_| println!("Clicked!");
  17. rsx! {
  18. div { class, id, {&children} }
  19. Component { a, b, c, children, onclick }
  20. Component { a, ..ComponentProps { a: 1, b: 2, c: 3, children: VNode::empty(), onclick: Default::default() } }
  21. }
  22. }
  23. #[component]
  24. fn Component(
  25. a: i32,
  26. b: i32,
  27. c: i32,
  28. children: Element,
  29. onclick: EventHandler<MouseEvent>,
  30. ) -> Element {
  31. rsx! {
  32. div { "{a}" }
  33. div { "{b}" }
  34. div { "{c}" }
  35. div { {children} }
  36. div { onclick }
  37. }
  38. }