dioxus/examples/spread.rs

44 lines
1.1 KiB
Rust
Raw Normal View History

//! This example demonstrates how to use the spread operator to pass attributes to child components.
//!
//! This lets components like the `Link` allow the user to extend the attributes of the underlying `a` tag.
//! These attributes are bundled into a `Vec<Attribute>` which can be spread into the child component using the `..` operator.
use dioxus::prelude::*;
fn main() {
2024-01-30 17:59:57 -08:00
let dom = VirtualDom::prebuilt(app);
let html = dioxus_ssr::render(&dom);
println!("{}", html);
}
fn app() -> Element {
2024-01-16 13:18:46 -06:00
rsx! {
2024-03-18 15:34:46 -07:00
SpreadableComponent {
2023-09-26 19:23:00 -05:00
width: "10px",
2023-09-27 10:05:02 -05:00
extra_data: "hello{1}",
extra_data2: "hello{2}",
2023-09-26 19:23:00 -05:00
height: "10px",
left: 1,
"data-custom-attribute": "value",
2023-09-26 19:23:00 -05:00
}
}
}
2024-01-14 15:21:19 -06:00
#[derive(Props, PartialEq, Clone)]
struct Props {
2023-09-26 19:23:00 -05:00
#[props(extends = GlobalAttributes)]
2024-01-14 15:21:19 -06:00
attributes: Vec<Attribute>,
2024-01-14 15:21:19 -06:00
extra_data: String,
2024-01-20 00:11:55 -08:00
2024-01-14 15:21:19 -06:00
extra_data2: String,
2023-09-26 19:23:00 -05:00
}
2024-01-20 00:11:55 -08:00
2024-03-18 15:34:46 -07:00
#[component]
fn SpreadableComponent(props: Props) -> Element {
2024-01-20 00:11:55 -08:00
rsx! {
audio { ..props.attributes, "1: {props.extra_data}\n2: {props.extra_data2}" }
}
}