1
0

children.rs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. //! Example: Children
  2. //! -----------------
  3. //!
  4. //! Dioxus supports passing children in from the parent. These children are allocated in the parent and just passed
  5. //! into the child. Components that pass in children may not be safely memoized, though in practice it's rare for a
  6. //! change in a parent to not result in a different set of children.
  7. //!
  8. //! In Dioxus, children can *only be a list*. Unlike React, you cannot pass in functions or arbitrary data. This is
  9. //! partially a limitation of having static types, but is rather intentional to encourage the use of attributes where
  10. //! arbitrary child data might normally be used. Check out the `function driven children` example for how to adopt your
  11. //! React pattern to Dioxus' semantics.
  12. //!
  13. //! Dioxus will let you use the `children` method more than once - and it's semantically *okay* - but you'll likely
  14. //! ruin your page if you try to clone elements in this way. Under the hood, Dioxus shares a "mounted ID" for each node,
  15. //! and mounting the same VNode in two places will overwrite the first mounted ID. This will likely lead to dead elements.
  16. //!
  17. //! In the future, this might become a runtime error, so consider it an error today.
  18. use dioxus::prelude::*;
  19. pub static Example: FC<()> = |(cx, props)| {
  20. cx.render(rsx! {
  21. div {
  22. Banner {
  23. p { "Some Content1" }
  24. }
  25. Banner {
  26. p { "Some Content2" }
  27. }
  28. }
  29. })
  30. };
  31. pub static Banner: FC<()> = |(cx, props)| {
  32. cx.render(rsx! {
  33. div {
  34. h1 { "This is a great banner!" }
  35. div { class: "content"
  36. {cx.children()}
  37. }
  38. footer { "Wow, what a great footer" }
  39. }
  40. })
  41. };