fragments.rs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //! Example: Fragments
  2. //! ------------------
  3. //!
  4. //! Dioxus can return multiple elements without a container through the use of the VNode called a "Fragment". Fragments do not
  5. //! have a mounted root and are inserted inline with their siblings. There are three ways of creating fragments as outlined
  6. //! in the examples below:
  7. //! - By returning multiple elements in Rsx!
  8. //! - By using the `Fragment` component
  9. //! - By using the fragment() method on the node factory
  10. use dioxus::prelude::*;
  11. pub fn Example(cx: Scope) -> Element {
  12. cx.render(rsx! {
  13. App1 {}
  14. App2 {}
  15. App3 {}
  16. })
  17. }
  18. // Returning multiple elements with rsx! or html!
  19. pub fn App1(cx: Scope) -> Element {
  20. cx.render(rsx! {
  21. h1 { }
  22. h2 { }
  23. h3 { }
  24. })
  25. }
  26. // Using the Fragment component
  27. pub fn App2(cx: Scope) -> Element {
  28. cx.render(rsx! {
  29. Fragment {
  30. div {}
  31. div {}
  32. "asd"
  33. }
  34. })
  35. }
  36. // Using the `fragment` method on the NodeFactory
  37. pub fn App3(cx: Scope) -> Element {
  38. cx.render(LazyNodes::new(move |fac| {
  39. fac.fragment_from_iter([
  40. fac.text(format_args!("A")),
  41. fac.text(format_args!("B")),
  42. fac.text(format_args!("A")),
  43. fac.text(format_args!("B")),
  44. ])
  45. }))
  46. }