Yew Component Lifecycle Explained
The Yew component lifecycle is similar to React but implemented using Rust's trait system. Understanding the component lifecycle is crucial for building efficient applications.
Main Lifecycle Methods
1. create()
- Timing: Called when the component is first created
- Purpose: Initialize component state
- Return Value: Returns the initial state of the component
- Example:
rustimpl Component for MyComponent { type Message = Msg; type Properties = Props; fn create(ctx: &Context<Self>) -> Self { Self { counter: 0, value: String::new(), } } }
2. view()
- Timing: Called whenever state or properties change
- Purpose: Render the component's UI
- Return Value: Returns
Htmltype - Example:
rustfn view(&self, ctx: &Context<Self>) -> Html { html! { <div class="container"> <h1>{ "Counter: " }{ self.counter }</h1> <button onclick={ctx.link().callback(|_| Msg::Increment)}> { "Increment" } </button> </div> } }
3. update()
- Timing: Called when a message is received
- Purpose: Handle state updates
- Return Value: Returns
boolindicating whether re-rendering is needed - Example:
rustfn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool { match msg { Msg::Increment => { self.counter += 1; true // Needs re-rendering } Msg::NoOp => false, // No re-rendering needed } }
4. changed()
- Timing: Called when component props change
- Purpose: Decide whether to update the component based on new props
- Return Value: Returns
boolindicating whether re-rendering is needed - Example:
rustfn changed(&mut self, ctx: &Context<Self>, _old_props: &Self::Properties) -> bool { // Only re-render when important properties change true }
5. rendered()
- Timing: Called after the component is rendered to the DOM
- Purpose: Perform DOM operations, initialize third-party libraries, etc.
- Example:
rustfn rendered(&mut self, ctx: &Context<Self>, first_render: bool) { if first_render { // Initialization operations after first render } }
6. destroy()
- Timing: Called when the component is removed from the DOM
- Purpose: Clean up resources, cancel subscriptions, etc.
- Example:
rustfn destroy(&mut self, ctx: &Context<Self>) { // Clean up timers, cancel network requests, etc. } }
Lifecycle Flow Diagram
shellCreate Component ↓ create() → Initialize State ↓ view() → Render UI ↓ rendered() → DOM Operations ↓ [Receive Message/Props Change] ↓ update() / changed() → Update State ↓ view() → Re-render ↓ rendered() → DOM Update ↓ [Component Unmount] ↓ destroy() → Clean Up Resources
Best Practices
- Avoid complex calculations in
view(): Move computation logic toupdate()or separate methods - Use
changed()wisely: Avoid unnecessary re-renders - Clean up resources promptly: Clean up timers, event listeners, etc. in
destroy() - Use
rendered()for DOM operations: Ensure DOM has finished rendering