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.

PREVIEW

hooks fired (newest first):

  • mount() - ran once before first render
class LifecycleDemo(Component):
    query: str = ""
    log: list = []

    def mount(self):
        self.log = ["mount() - ran once before first render"]

    def updated_query(self, value):
        entry = f"updated_query('{value}')"
        self.log = ([entry] + self.log)[:5]

    def inline_template(self):
        return """
        <div class="...">
            <input lace:model.live="query" placeholder="type to fire updated_query…" class="...">
            <p class="...">hooks fired (newest first):</p>
            <ul class="...">
                {% for entry in log %}<li class="...">{{ entry }}</li>{% endfor %}
            </ul>
        </div>
        """

Reacting to a specific property

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

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):

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:

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.

View this page as markdown - handy as context for your AI tools. Full index at /llms.txt.

Theme
Accent
Ink