Module ruukh::component

source ·
Expand description

This module defines traits the user needs to implement a Ruukh Component.

The main traits of concern for a user are Lifecycle and Render.

The other trait which is required is the uber important Component trait, which is automatically implemented by using the #[component] attribute on top of the component struct. You are advised against to implement it yourself other than to learn how everything wires up.

So onto implementing the Lifecycle and Render trait. By the way if you desire not to write the implementation of Lifecycle yourself, you may write #[derive(Lifecycle)] on your component struct.

Example

#[component]
struct Help;

impl Lifecycle for Help {}

impl Render for Help {
    fn render(&self) -> Markup<Self> {
        html! {
            <div>
                "This is help section"
            </div>
            <div>
                "There are a lot of divs here. Here comes a "<button>"Button"</button>
            </div>
        }
    }
}

The lifecycle implementation provided here is bare bones. Actually, lifecycle provides a default implementation which does nothing. If you want to provide a much more specific implementation, you can selectively implement its methods.

Example

impl Lifecycle for Help {
    fn created(&self) {
        println!("It got created just now!");
    }
}

Note: Docs on component macros are located here.

Structs

Stores the state of the component along with the flags to identify whether the props and state are dirty. Also provides a mechanism to notify the app of state changes.

Traits

Trait to define a component. You do not need to implement this trait. Auto implement this trait by using #[component] on a component struct (which also does other magical stuff).
Trait to convert from a event props to a events type.
The lifecycle of a stateful component.
Trait to render a view for the component.
Trait to allow mutatation of a component state.
Trait to get a component setter to be used within 'static closures.

Type Definitions

A void component to be used as a render context for a root component. Simply the parent of the root.