Component Lifecycle
Understanding the lifecycle of a Snakeskin component is crucial for managing state and side effects.
The Lifecycle Diagram
- Initialization (
__init__): Props are received, initial state is set. - Mount (
on_mount): Component is added to the DOM. - Update (
on_update): State or props change, re-render occurs. - Unmount (
on_unmount): Component is removed from the DOM.
Hooks
on_mount(self)
Called immediately after the component is inserted into the DOM.
python
def on_mount(self):
print("Component mounted!")
self.fetch_data() on_update(self, prev_props, prev_state)
Called when state or props change.
python
def on_update(self, prev_props, prev_state):
if self.state['count'] != prev_state['count']:
print(f"Count changed to {self.state['count']}") on_unmount(self)
Called just before component removal. Cleanup timers or subscriptions here.
python
def on_unmount(self):
clearInterval(self.timer)