Skip to main content

Component

Trait Component 

Source
pub trait Component: Sized + 'static {
    type Props: Props;
    type State: State;

    // Required methods
    fn initial_state(props: &Self::Props) -> Self::State;
    fn render(props: &Self::Props, state: &Self::State) -> RenderOutput;
    fn on_event(
        props: &Self::Props,
        state: &mut Self::State,
        event: ComponentEvent,
    ) -> EventResult;

    // Provided methods
    fn props_changed(
        _old_props: &Self::Props,
        _new_props: &Self::Props,
        _state: &mut Self::State,
    ) -> bool { ... }
    fn cleanup(_props: &Self::Props, _state: &mut Self::State) { ... }
}
Expand description

The core trait all NexusStratum UI components implement.

Component defines the framework-agnostic lifecycle of a UI component:

  • Initialize state from props
  • Render a framework-agnostic description of the component
  • Handle events and update state

Framework adapters (stratum-leptos, stratum-dioxus) bridge this trait to each framework’s specific rendering and reactivity model.

Required Associated Types§

Source

type Props: Props

The property type for this component.

Source

type State: State

The internal state type for this component.

Required Methods§

Source

fn initial_state(props: &Self::Props) -> Self::State

Create the initial state from the given props.

Source

fn render(props: &Self::Props, state: &Self::State) -> RenderOutput

Produce a framework-agnostic render description.

The returned RenderOutput describes the component’s attributes, CSS classes, ARIA attributes, and children. Framework adapters translate this into actual DOM/VDOM elements.

Source

fn on_event( props: &Self::Props, state: &mut Self::State, event: ComponentEvent, ) -> EventResult

Handle an event and optionally mutate state.

Returns an EventResult indicating whether the event should be prevented, stopped, or if state changed (triggering re-render).

Provided Methods§

Source

fn props_changed( _old_props: &Self::Props, _new_props: &Self::Props, _state: &mut Self::State, ) -> bool

Called when props change. Returns true if state needs updating.

Default implementation always returns false (state is independent of props). Override for components where prop changes should sync to state.

Source

fn cleanup(_props: &Self::Props, _state: &mut Self::State)

Called before the component is unmounted.

Default implementation is a no-op. Override for cleanup logic.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§