dioxus/examples/spread.rs

42 lines
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-31 01:59:57 +00:00
let dom = VirtualDom::prebuilt(app);
let html = dioxus_ssr::render(&dom);
println!("{}", html);
}
fn app() -> Element {
2024-01-16 19:18:46 +00:00
rsx! {
2024-01-16 17:04:37 +00:00
spreadable_component {
2023-09-27 00:23:00 +00:00
width: "10px",
2023-09-27 15:05:02 +00:00
extra_data: "hello{1}",
extra_data2: "hello{2}",
2023-09-27 00:23:00 +00:00
height: "10px",
2024-01-06 14:58:49 +00:00
left: 1
2023-09-27 00:23:00 +00:00
}
}
}
2024-01-14 21:21:19 +00:00
#[derive(Props, PartialEq, Clone)]
struct Props {
2023-09-27 00:23:00 +00:00
#[props(extends = GlobalAttributes)]
2024-01-14 21:21:19 +00:00
attributes: Vec<Attribute>,
2024-01-14 21:21:19 +00:00
extra_data: String,
2024-01-20 08:11:55 +00:00
2024-01-14 21:21:19 +00:00
extra_data2: String,
2023-09-27 00:23:00 +00:00
}
2024-01-20 08:11:55 +00:00
fn spreadable_component(props: Props) -> Element {
rsx! {
audio { ..props.attributes, "1: {props.extra_data}\n2: {props.extra_data2}" }
}
}