pub trait Component: Sized {
type State: Clone;
type Message: Clone;
type Output: Clone;
// Required methods
fn init() -> Self::State;
fn update(
state: &mut Self::State,
msg: Self::Message,
) -> Option<Self::Output>;
fn view(state: &Self::State, frame: &mut Frame<'_>, area: Rect);
}Expand description
A composable UI component with its own state and message handling.
Components are the building blocks of complex TUI applications. Each component manages its own state, handles its own messages, and renders to a specific area of the screen.
§Associated Types
State: The component’s internal state. Should beClonefor testing.Message: Messages the component can receive from its parent or from user interaction.Output: Messages the component emits to communicate with its parent. Use()if the component doesn’t need to communicate outward.
§Design Pattern
Components follow the same TEA pattern as App, but
at a smaller scale:
- Parent sends
Messageto component - Component updates its
State - Component optionally emits
Outputto parent - Component renders itself to its designated area
Required Associated Types§
Sourcetype State: Clone
type State: Clone
The component’s internal state type.
This should contain all data needed to render the component.
Derive Clone for testing and state snapshots.
Required Methods§
Sourcefn init() -> Self::State
fn init() -> Self::State
Initialize the component state.
Returns the initial state for this component.
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.