pub trait LiveComponent:
Send
+ Sync
+ 'static {
// Required methods
fn render(&self) -> String;
fn handle_event<'a>(
&'a mut self,
event: &'a str,
params: &'a HashMap<String, String>,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>>;
// Provided method
fn mount(&mut self) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> { ... }
}Expand description
Trait for server-rendered live components.
A LiveComponent holds state, renders HTML, and handles events from the browser.
When an event is received via WebSocket, handle_event is called, the state
is updated, and render produces new HTML that is sent back to the client.
SECURITY: The HTML returned by render() is sent directly to the browser.
Always use html_escape() for any user-provided content to prevent XSS.
ⓘ
struct Counter {
count: i32,
}
impl LiveComponent for Counter {
fn render(&self) -> String {
format!(r#"
<div>
<span>{}</span>
<button lw-click="increment">+</button>
<button lw-click="decrement">-</button>
</div>
"#, self.count)
}
fn handle_event<'a>(
&'a mut self,
event: &'a str,
_params: &'a HashMap<String, String>,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> {
Box::pin(async move {
match event {
"increment" => self.count += 1,
"decrement" => self.count -= 1,
_ => {}
}
})
}
}