FerroUI/ docs

Search guides, controls, Rust APIs, and XAML.

    GitHub ↗

    component

    proc macro · ferroui-view-macros · v0.5.0

    attr macro component!
    

    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.

    #[component(rerender)] makes a component that runs again: its function runs whenever a signal it read changes or its parent runs again with other parameters, the elements it returns are patched into the controls it made before, and its state is kept in hooks (use_signal, use_effect, ...). Its parameters are cloned for each run, so they are Clone. When they are also PartialEq it is memoized: a parent that runs again with equal parameters does not run it again. #[component(rerender, memo)] requires that (a compile error otherwise), and #[component(rerender, always)] runs it with its parent every time.

    View source

    Availability

    Extracted for aarch64-apple-darwin. Cargo features are listed in the crate index.