Skip to main content

Component

Trait Component 

Source
pub trait Component:
    Send
    + Sync
    + 'static {
    type State: Send + Sync + Default + 'static;

    // Provided method
    fn update(
        &self,
        hooks: &mut Hooks<Self, Self::State>,
        state: &Self::State,
        children: Elements,
    ) -> Elements
       where Self: Sized { ... }
}
Expand description

A component that can render itself into a terminal region.

Most users should define components with the #[component] and #[props] attribute macros rather than implementing this trait directly:

#[props]
struct CardProps {
    title: String,
    #[default(true)]
    visible: bool,
}

#[component(props = CardProps, children = Elements)]
fn card(props: &CardProps, children: Elements) -> Elements {
    element! {
        View(border: BorderType::Rounded, title: props.title.clone()) {
            #(children)
        }
    }
}

The #[component] macro generates an impl Component with an update() override that calls the function once per cycle with real hooks and real children.

§Direct implementation

Implement this trait directly only for framework primitives that need raw buffer access (Canvas) or layout chrome (View). Most methods are #[doc(hidden)] — they exist for the framework and primitives, not for typical component authors.

Required Associated Types§

Source

type State: Send + Sync + Default + 'static

State type for this component. The framework wraps it in Tracked<S> for automatic dirty detection.

Provided Methods§

Source

fn update( &self, hooks: &mut Hooks<Self, Self::State>, state: &Self::State, children: Elements, ) -> Elements
where Self: Sized,

Combined lifecycle and view in a single call.

Called by the framework during reconciliation. Collects hooks and produces the element tree in one pass, so the component function runs exactly once per cycle.

The default implementation calls lifecycle() then view() separately — correct for hand-written impl Component primitives. The #[component] macro overrides this to call the user function once with real hooks and real children.

Implementors§