dioxus/examples/shorthand.rs

37 lines
951 B
Rust
Raw Normal View History

2024-01-10 22:41:40 -08:00
use dioxus::prelude::*;
fn main() {
launch_desktop(app);
2024-01-10 22:41:40 -08:00
}
fn app() -> Element {
2024-01-10 22:41:40 -08:00
let a = 123;
let b = 456;
let c = 789;
2024-01-10 22:52:41 -08:00
let class = "class";
let id = "id";
2024-01-10 22:41:40 -08:00
// todo: i'd like it for children on elements to be inferred as the children of the element
// also should shorthands understand references/dereferences?
// ie **a, *a, &a, &mut a, etc
2024-01-16 13:18:46 -06:00
let children = rsx! { "Child" };
let onclick = move |_| println!("Clicked!");
2024-01-10 23:42:36 -08:00
2024-01-16 13:18:46 -06:00
rsx! {
2024-01-10 23:52:38 -08:00
div { class, id, {&children} }
Component { a, b, c, children, onclick }
Component { a, ..ComponentProps { a: 1, b: 2, c: 3, children: None, onclick: Default::default() } }
2024-01-10 22:41:40 -08:00
}
}
#[component]
fn Component(a: i32, b: i32, c: i32, children: Element, onclick: EventHandler) -> Element {
2024-01-16 13:18:46 -06:00
rsx! {
2024-01-10 22:41:40 -08:00
div { "{a}" }
div { "{b}" }
div { "{c}" }
2024-01-10 23:52:38 -08:00
div { {children} }
div { onclick: move |_| onclick.call(()) }
2024-01-10 22:41:40 -08:00
}
}