Trait Component

Source
pub trait Component: Sized {
    type Message: 'static;
    type Properties: Hash + 'static;

    // Required methods
    fn new(props: Self::Properties) -> Self;
    fn view(&self, behavior: &mut impl Behavior<Self>) -> VNode;
    fn update(&mut self, message: Self::Message) -> bool;
}
Expand description

Trait for defining custom component.

It adapts a MVC pattern. Model is the fields and state of the component. It is initialized by new function using Properties. View is the function view that returns a view of an application. Controller is the function update that updates the model based on the Message.

Required Associated Types§

Source

type Message: 'static

Type to describe the message that can be sent to the component. It is used to update the model of the component.

Source

type Properties: Hash + 'static

Type to describe the properties that can be passed to the component. It is used to initialize the model of the component.

Required Methods§

Source

fn new(props: Self::Properties) -> Self

Function that creates a new instance of the component therefore initialize a model using Properties.

Source

fn view(&self, behavior: &mut impl Behavior<Self>) -> VNode

Function that returns a view of the component. It uses Behavior to create Callbacks that are responsible to send Messages to the component. Defining the views is done using rsx macro. Using the macro is not mandatory but it is recommended and makes in unnecessary to know the complex structure of VNode.

Source

fn update(&mut self, message: Self::Message) -> bool

Function that updates the model of the component based on the Message. It returns a boolean that indicates if the rerender of the component is necessary. Meaning whether the view of the component should be updated or not.

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§