pub trait Component: ComponentKey + 'static {
// Required method
fn render(&self) -> impl IntoElement;
// Provided method
fn render_key(&self) -> DiffKey { ... }
}Expand description
Encapsulate reusable pieces of UI by using the Component trait. Every Component creates a new layer of state in the app, meaning that implementors of Component can make use of hooks in their Component::render method.
#[derive(PartialEq)]
struct ReusableCounter {
pub init_number: u8,
}
impl Component for ReusableCounter {
fn render(&self) -> impl IntoElement {
let mut number = use_state(|| self.init_number);
label()
.on_press(move |_| {
*number.write() += 1;
})
.text(number.read().to_string())
}
}Required Methods§
fn render(&self) -> impl IntoElement
Provided Methods§
fn render_key(&self) -> DiffKey
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.