#[derive(Model)]
{
// Attributes available to this derive:
#[state]
#[init]
#[update]
#[view]
#[model]
}
Expand description
Derive macro for implementing the Model trait.
This macro generates a Model trait implementation that delegates to
inherent methods named init, update, and view on your struct.
§Requirements
- Applied to a named struct (not enum, union, tuple struct, or unit struct)
- Struct must implement
init(&self) -> Option<Cmd>as an inherent method - Struct must implement
update(&mut self, msg: Message) -> Option<Cmd> - Struct must implement
view(&self) -> String
§Field Attributes
#[state]- Track field for change detection#[state(eq = "fn_name")]- Use custom equality function#[state(skip)]- Exclude from change detection#[state(debug)]- Log changes in debug builds
§Example
ⓘ
use bubbletea::{Cmd, Message, Model};
#[derive(Model)]
struct Counter {
#[state]
count: i32,
}
impl Counter {
fn init(&self) -> Option<Cmd> {
None
}
fn update(&mut self, msg: Message) -> Option<Cmd> {
if let Some(&delta) = msg.downcast_ref::<i32>() {
self.count += delta;
}
None
}
fn view(&self) -> String {
format!("Count: {}", self.count)
}
}