Context
wybthon.context¶
context
¶
Context system for passing values through the component tree.
Context values are stored on the reactive ownership tree.
use_context walks up the owner chain to find
the nearest Provider, eliminating the need for a
separate render-time stack.
The provider's value is wrapped in a Signal, so
descendants that read it inside a tracking scope (e.g., a reactive hole
or create_effect) automatically re-run when
the provider's value changes, without re-mounting any subtrees.
See Also
Classes:
| Name | Description |
|---|---|
Context |
Opaque context identifier paired with a default value. |
Functions:
| Name | Description |
|---|---|
create_context |
Create a new |
use_context |
Read the current value for |
Provider |
Context provider component. |
Context
dataclass
¶
Opaque context identifier paired with a default value.
Returned by create_context. Treat the
object as opaque: pass it to a Provider and
to use_context but do not rely on its
fields.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
int
|
Process-unique integer used as the storage key on owner scopes. |
default |
Any
|
Value returned by |
create_context
¶
Create a new Context with the given default value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default
|
Any
|
Value returned by |
required |
Returns:
| Type | Description |
|---|---|
Context
|
A fresh |
Context
|
unique id, even when |
use_context
¶
Read the current value for ctx from the ownership tree.
Walks up the owner chain looking for the nearest provider that stored a value for this context. The returned value is unwrapped from the provider's signal so callers always observe the current value. When invoked inside a tracking scope (a reactive hole or effect), the dependency on the provider signal is recorded so the scope re-runs whenever the provider updates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
Context
|
The context token created by
|
required |
Returns:
| Type | Description |
|---|---|
Any
|
The nearest provider's current value, or |
Any
|
|
Provider
¶
Context provider component.
Renders its children transparently. The reconciler stores value
as a Signal on this component's ownership
scope so that descendants can find it via
use_context and react to updates with
fine-grained precision.
Children are wrapped in a reactive hole so updates from the parent (for example a router swapping the matched route component) flow into the subtree even though the provider body itself runs only once.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
props
|
Any
|
The component's props with the following keys:
|
required |
Returns:
| Type | Description |
|---|---|
Any
|
A reactive |
Any
|
provider's children. |
create_context(default) -> Context¶
Create a new context with a unique ID and a default value. The default
is returned by use_context when no ancestor Provider supplies a
value.
use_context(ctx) -> Any¶
Read the current value for ctx by walking up the ownership tree
from _current_owner. Searches each owner's _context_map for the
context ID; returns the first match, or ctx.default if none is found.
Can be called during a component's setup phase, inside a render function, or inside an effect: anywhere that runs under a reactive owner.
Provider(props) -> VNode¶
Function component that provides a context value to its subtree. The
reconciler sets the value on the provider's _ComponentContext via
_set_context, making it visible to all descendant owners.
Props:
context: aContextobject created bycreate_context.value: the value to provide.children: child VNodes.
Nested providers for the same context shadow outer ones.