spread.rs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. "data-custom-attribute": "value",
  20. }
  21. }
  22. }
  23. #[derive(Props, PartialEq, Clone)]
  24. struct Props {
  25. #[props(extends = GlobalAttributes)]
  26. attributes: Vec<Attribute>,
  27. extra_data: String,
  28. extra_data2: String,
  29. }
  30. #[component]
  31. fn SpreadableComponent(props: Props) -> Element {
  32. rsx! {
  33. audio { ..props.attributes, "1: {props.extra_data}\n2: {props.extra_data2}" }
  34. }
  35. }