Skip to main content

Component

Trait Component 

Source
pub trait Component: 'static {
    type State: 'static;

    // Required methods
    fn init(&self) -> Self::State;
    fn build(&self, state: &mut Self::State) -> impl View<Self::State>;
}
Expand description

A piece of UI with retained local state (Flutter’s StatefulWidget analog).

A Component declares an associated Component::State type, seeds it once with Component::init, and produces its subtree with Component::build, which receives &mut State so the build can read (and the subtree’s events can mutate) the retained state. The subtree is any impl View over the component’s own State — the outer state type never appears. Erasure happens at the API boundary: ComponentWidget wraps whatever build returns in an AnyView before storing it, and because AnyView::new is idempotent a body that already returns an AnyView (e.g. any(..)) is not boxed a second time.

Required Associated Types§

Source

type State: 'static

The retained local state this component owns across rebuilds.

Required Methods§

Source

fn init(&self) -> Self::State

Create the component’s initial local state. Runs exactly once, when the ComponentWidget is first built, under the component’s Owner (so on_cleanup/provide_context registered here bind to that owner).

A root component (the one an entry macro or frust::run drives) has no enclosing ComponentWidget and so no per-component owner of its own: the shell instead runs its init/build under the shell’s root Owner, which lives for the whole process and is never disposed. Its on_cleanup/provide_context therefore bind to that root owner — the context is visible to the entire tree, and cleanups run at process exit rather than on teardown (there is no teardown for the root).

Source

fn build(&self, state: &mut Self::State) -> impl View<Self::State>

Produce the component’s subtree from its current local state.

Re-run on every rebuild (local state may have changed even when the component value is equal). The returned view is erased into an AnyView by the hosting ComponentWidget and diffed against the previous one exactly like RenderRoot’s root view is. Implementations declare the return type as impl View<Self::State>; returning a concrete view type or an any(..) value both satisfy it.

The opaque return type captures the &self and &mut State borrows, so a caller that hands the result out of a closure (a root driver’s move |state| .. build closure) erases it first with AnyView::new(root.build(state)).

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§