spread.rs 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. spreadable_component {
  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. fn spreadable_component(props: Props) -> Element {
  30. rsx! {
  31. audio { ..props.attributes, "1: {props.extra_data}\n2: {props.extra_data2}" }
  32. }
  33. }