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) -> AnyView<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 an AnyView over the component’s own State — the outer state type never appears.

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) -> AnyView<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 AnyView is diffed against the previous one exactly like RenderRoot’s root view is.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§