Component
wybthon.component¶
component
¶
The @component decorator and the Component type it produces.
Wybthon components run once: the body executes a single time when the component mounts and returns its markup. A component declares its inputs as ordinary parameters, which are its props:
@component
def Counter(label: str = "Count", initial: int = 0):
count, set_count = create_signal(untrack(lambda: initial))
def increment():
set_count(lambda n: n + 1)
return html(t"<div><p>{label}: {count()}</p><button onclick={increment}>+</button></div>")
render(Counter(initial=10), "#app")
Wybthon's compiler turns every read of a parameter in the body into a
tracked read of the parent's current value, as reading a property of
Solid's props object is. So {label} in the markup updates when the
parent passes an expression that changes, and a handler that reads a
parameter reads its latest value. Reading one at the top level of the
body reads it once, as destructuring Solid's props does; dev mode warns
when the value was reactive.
Calling a component returns a node, and so does a component tag in a
template (<{Counter} initial={5} />). Type checkers see the call as a
call of the function, so missing, mistyped, and unknown props are errors
without a plugin. Content arrives as the children parameter: a tag's
markup, the children keyword, or item syntax on the call,
Card(title="Hi")[html(t"<p>Body</p>")]. A **rest parameter collects
the props the component doesn't declare, as a reactive view that
spreads onto an element.
Classes:
| Name | Description |
|---|---|
Component |
A run-once component produced by |
Functions:
| Name | Description |
|---|---|
declared_props |
The props a component declares by name, or |
builtin |
Mark a built-in that renders markup, so compiled templates pass it getters (internal). |
component |
Declare a function as a run-once Wybthon component. |
Component
¶
declared_props
¶
The props a component declares by name, or None when it can't say (internal).
A component with a **rest parameter also accepts props it doesn't
declare, but its declared names are still returned: the router passes
a route's component only the route props (params, search, data,
children) it declares, so they never land in its **rest spread.
A lazy component, a built-in, and a plain function used as a tag
return None.
builtin
¶
Mark a built-in that renders markup, so compiled templates pass it getters (internal).
Show, For, Loading, and the other flow and boundary functions
aren't @components, but a call of one in a template should build it
with getters for its dynamic arguments, as a component call does.
component
¶
Declare a function as a run-once Wybthon component.
The function's parameters are its props. Calling the result returns a node for the tree; type checkers check the call against the function's own signature.
What's in this module¶
The @component decorator turns a function into a
run-once Component. The body executes a single
time when the component mounts and returns a tree, usually an
html template. Its inputs are ordinary function
parameters, the counterpart of the properties of Solid's props
object: the compiler rewrites every read of a parameter in the body
into a tracked read of the parent's current value, so later changes
flow into the places that read it without re-running the body. Calling
a component returns a VNode, so trees compose like any other node.
| Name | Description |
|---|---|
component |
Decorator producing a Component from a function whose parameters are its props. |
Component |
Callable wrapper; Counter(label="Clicks") returns a VNode. Type checkers see the decorated function itself, returning a Node, and check calls against its signature. |
builtin |
Internal: marks a built-in that renders markup (Show, For, Loading, ...) so a compiled template builds it with getters for its dynamic arguments, as it does a component. |
Declaring props¶
- Parameters are props (
title: str). Readingtitlereturns the value and tracks it; in a template,{title}is dynamic, unlike other bare names. The parent may pass a value, an accessor, or a getter (what the compiler makes of<{Card} title={user().name}>); getters and accessors are unwrapped on read. - Defaults are ordinary parameter defaults:
step: int = 1. Useprop(default_factory=...)for a mutable default; the factory runs once per instance. - Callable parameters (
Callable[...]orAccessor[...], alone or in a union withNone) read as what the parent passed, never calling it, so callbacks and accessors you mean to keep arrive intact. - Children arrive as a
childrenparameter, annotated withChildren:children: Children = None. Pass children as the content of a component tag in a template (<{Card} title="Hi">...</{Card}>), as thechildrenkeyword, or with item syntax (Card(title="Hi")[...]). **rest(any name, such as**attrs: Any) collects the props the component doesn't declare, as a reactive, read-only view that spreads onto an element (<div {attrs}>).*argsand positional-only parameters (/) aren't allowed.- Arguments bind as in a Python call:
Greeting("Ada")isGreeting(name="Ada"). Positional arguments are never children. - Parameters are read-only: assigning to one,
del, rebinding it withfor,import, orexcept, or declaring itnonlocalis aSyntaxErrorwhen the module is compiled. - A component with no inputs takes no parameters.
In dev mode, an unknown prop or a missing required one raises
TypeError at the call site, and so does calling a component with
parameters whose module wasn't compiled. merge and
omit combine or trim views (dicts, **rest views,
and other views) before you spread them: merge({"type": "button"}, attrs).
from collections.abc import Callable
from typing import Any
from wybthon import Children, component, create_signal, html, untrack
@component
def Counter(label: str = "Count", initial: int = 0, on_change: Callable[[int], None] | None = None):
count, set_count = create_signal(untrack(lambda: initial)) # a one-time read
def increment():
set_count(lambda n: n + 1)
if on_change is not None:
on_change(count.peek() + 1)
return html(t"""
<div>
<p>{label}: {count()}</p>
<button onclick={increment}>+</button>
</div>
""")
@component
def Card(title: str, children: Children = None, **attrs: Any):
return html(t'<div class="card" {attrs}><h2>{title}</h2>{children}</div>')
heading, set_heading = create_signal("Hello")
Counter(initial=5, label="Clicks", on_change=print)
html(t"<{Card} title={heading()} id='intro'><p>body text</p></{Card}>") # title stays live
html(t"{Card(title=heading(), children=html(t'<p>body text</p>'))}") # the same, type-checked
Reading a parameter or signal at the top level of the body reads it
once, as destructuring props does in Solid, and warns in dev mode when
the value was reactive; read it inside a template expression, memo, or
effect, or make the one-time read explicit with
untrack. A prop the parent passed as a constant
can't change, so reading one doesn't warn.
Components don't take a key. To remount a component when an identity
changes, show it through a keyed Show:
{Show(doc_id(), lambda current: DocumentView(doc_id=current), keyed=True)}.
Using components in templates¶
Call a component inside an interpolation, {Counter(initial=5)}, to
keep full type checking of its props; the compiler still passes each
dynamic argument as a getter. The tag form,
<{Card} title="Hi">...</{Card}> or <{Card} />, passes attributes as
keyword props (a hyphen becomes an underscore) and nested markup as
children; type checkers can't see inside the template string, so
wyb check checks its props as the equivalent call, and dev mode
checks them at run time. See
Templates.
What a component costs¶
A component is only patched with new props when a reactive hole
re-renders a tree that contains it, or when render runs again into the
same container. Everywhere else (inside a template, another component's
output, a list row, or a Show branch) its props never change, so:
- reading a prop that the parent passed as a constant subscribes to nothing;
- a prop passed as a getter or accessor is read through, so the reader subscribes to that source directly;
- a binding that reads nothing reactive never allocates a computation.
A component whose props never change costs about what its markup costs.
Components that can be patched track one version signal per instance,
created on the first tracked read. Use is_static
to ask whether the parent passed a plain value: is_static(title),
passing the parameter itself, or is_static(attrs, "key") for a key of
a view.
A plain, undecorated function used as a tag, as in h(fn, props),
receives the props dictionary and runs again when it's patched with
changed props. Use @component when you want typed, reactive props.