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. // Returning multiple elements with rsx! or html!
  12. static App1: Component<()> = |cx| {
  13. cx.render(rsx! {
  14. h1 { }
  15. h2 { }
  16. h3 { }
  17. })
  18. };
  19. // Using the Fragment component
  20. static App2: Component<()> = |cx| {
  21. cx.render(rsx! {
  22. Fragment {
  23. div {}
  24. div {}
  25. "asd"
  26. }
  27. })
  28. };
  29. // Using the `fragment` method on the NodeFactory
  30. static App3: Component<()> = |cx| {
  31. cx.render(LazyNodes::new(move |fac| {
  32. fac.fragment_from_iter([
  33. fac.text(format_args!("A")),
  34. fac.text(format_args!("B")),
  35. fac.text(format_args!("A")),
  36. fac.text(format_args!("B")),
  37. ])
  38. }))
  39. };
  40. pub static Example: Component<()> = |cx| {
  41. cx.render(rsx! {
  42. App1 {}
  43. App2 {}
  44. App3 {}
  45. })
  46. };