Skip to content

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 Context with the given default value.

use_context

Read the current value for ctx from the ownership tree.

Provider

Context provider component.

Context dataclass

Context(id: int, default: Any)

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 use_context when no enclosing provider is found.

create_context

create_context(default: Any) -> Context

Create a new Context with the given default value.

Parameters:

Name Type Description Default
default Any

Value returned by use_context when no provider is found above the consumer.

required

Returns:

Type Description
Context

A fresh Context token. Each call returns a context with a

Context

unique id, even when default is the same.

use_context

use_context(ctx: Context) -> Any

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 create_context.

required

Returns:

Type Description
Any

The nearest provider's current value, or

Any

ctx.default when no provider exists above the caller.

Provider

Provider(props: Any) -> Any

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:

  • context (Context): The context being provided.
  • value: The current value to expose to descendants.
  • children: A VNode or list of VNodes rendered transparently.
required

Returns:

Type Description
Any

A reactive VNode subtree containing the

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: a Context object created by create_context.
  • value: the value to provide.
  • children: child VNodes.

Nested providers for the same context shadow outer ones.