spread.rs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. //! This example demonstrates how to use the spread operator to pass attributes to child components.
  2. //!
  3. //! This lets components like the `Link` allow the user to extend the attributes of the underlying `a` tag.
  4. //! These attributes are bundled into a `Vec<Attribute>` which can be spread into the child component using the `..` operator.
  5. use dioxus::prelude::*;
  6. fn main() {
  7. let dom = VirtualDom::prebuilt(app);
  8. let html = dioxus_ssr::render(&dom);
  9. println!("{}", html);
  10. }
  11. fn app() -> Element {
  12. rsx! {
  13. SpreadableComponent {
  14. width: "10px",
  15. extra_data: "hello{1}",
  16. extra_data2: "hello{2}",
  17. height: "10px",
  18. left: 1
  19. }
  20. }
  21. }
  22. #[derive(Props, PartialEq, Clone)]
  23. struct Props {
  24. #[props(extends = GlobalAttributes)]
  25. attributes: Vec<Attribute>,
  26. extra_data: String,
  27. extra_data2: String,
  28. }
  29. #[component]
  30. fn SpreadableComponent(props: Props) -> Element {
  31. rsx! {
  32. audio { ..props.attributes, "1: {props.extra_data}\n2: {props.extra_data2}" }
  33. }
  34. }