Trait yew::html::Component [−][src]
pub trait Component: Sized + 'static {
type Message: 'static;
type Properties: Properties;
fn create(ctx: &Context<Self>) -> Self;
fn view(&self, ctx: &Context<Self>) -> Html;
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool { ... }
fn changed(&mut self, ctx: &Context<Self>) -> bool { ... }
fn rendered(&mut self, ctx: &Context<Self>, first_render: bool) { ... }
fn destroy(&mut self, ctx: &Context<Self>) { ... }
}
Expand description
Components are the basic building blocks of the UI in a Yew app. Each Component chooses how to display itself using received props and self-managed state. Components can be dynamic and interactive by declaring messages that are triggered and handled asynchronously. This async update mechanism is inspired by Elm and the actor model used in the Actix framework.
Associated Types
Messages are used to make Components dynamic and interactive. Simple
Component’s can declare their Message type to be ()
. Complex Component’s
commonly use an enum to declare multiple Message types.
type Properties: Properties
type Properties: Properties
The Component’s properties.
When the parent of a Component is re-rendered, it will either be re-created or
receive new properties in the context passed to the changed
lifecycle method.
Required methods
Components define their visual layout using a JSX-style syntax through the use of the
html!
procedural macro. The full guide to using the macro can be found in Yew’s
documentation.
Note that view()
calls do not always follow a render request from update()
or
changed()
. Yew may optimize some calls out to reduce virtual DOM tree generation overhead.
The create()
call is always followed by a call to view()
.
Provided methods
Called when a new message is sent to the component via it’s scope.
Components handle messages in their update
method and commonly use this method
to update their state and (optionally) re-render themselves.
Returned bool indicates whether to render this Component after update.
Called when properties passed to the component change
Returned bool indicates whether to render this Component after changed.
The rendered
method is called after each time a Component is rendered but
before the browser updates the page.
Note that rendered()
calls do not always follow a render request from update()
or
changed()
. Yew may optimize some calls out to reduce virtual DOM tree generation overhead.
The create()
call is always followed by a call to view()
and later rendered()
.