fragments.rs 1.2 KB

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