# Lifecycle Hooks

Run code at specific points in a component's lifecycle. Interlace has three hooks: define any of them
as a method on your component and the framework calls it for you.

## Available hooks

- **`mount(self)`** - runs once, before the first render. Use it for setup: load data, derive initial state from props.
- **`updated(self, prop, value)`** - runs after *any* bound property changes. `prop` is the property name (dotted for a nested path, e.g. `"config.theme"`), `value` is the new value.
- **`updated_<prop>(self, value)`** - runs after one *specific* property changes. Name the method after the property: `updated_email` fires when `email` changes.

That is the whole set: `mount` for setup, the two `updated` hooks for reacting to changes. Both
`updated` hooks run server-side on the request that carries the change.

```interlace-demo
LifecycleDemo
```

## Reacting to a specific property

Define `updated_<name>` to respond to just that one property. It receives the new value:

```python
class Profile(Component):
    email: str = ""
    email_verified: bool = False

    def updated_email(self, value):
        # New address, require re-verification
        self.email_verified = False
```

For a **nested** property (a dict or object path), the hook also receives the sub-path that changed,
so the signature is `(path, value)`:

```python
class Settings(Component):
    config: dict = {"theme": "light", "lang": "en"}

    def updated_config(self, key, value):
        # lace:model="config.theme" -> key = "theme", value = "dark"
        self.save_preference(key, value)
```

## Reacting to any change

`updated` fires for every bound property, with the (dotted) property name and the new value. Good for
cross-cutting concerns like a dirty flag:

```python
class Form(Component):
    name: str = ""
    email: str = ""
    has_unsaved_changes: bool = False

    def updated(self, prop, value):
        self.has_unsaved_changes = True
```

> These three are the complete set, there is no before-change, `hydrate`, `rendered`, or `complete`
> hook. Those names (and `updating`, `calling`, ...) are reserved, so don't use them as property names.
