Skip to main content

component

Attribute Macro component 

Source
#[component]
Expand description

A component: a function whose parameters are its properties, with a builder so that rsx! and the builders use it like a control.

ⓘ
#[component]
fn Counter(initial: i32, #[prop(default = 1)] step: i32) -> Element {
    let count = signal(initial);
    rsx! { Button { on_click: move |_| count.update(|c| *c += step), {move || count.get()} } }
}

rsx! { Counter { initial: 5 } }        // or counter().initial(5)

The name is in upper camel case. Besides the function, the macro declares a type of the name (what rsx! names: Counter { .. }) and a builder function of the name in snake case (counter()), with a setter per parameter that takes what converts into its type (IntoValue: the conversions of the setters of controls). A parameter is required unless it has #[prop(default)] (the default of its type), #[prop(default = expr)], or an Option type. A parameter named children of type Element takes the children of the element (.child(..)). The function runs once, when the element is built.