Context
wybthon.context¶
context
¶
Context: pass values down the tree without threading props.
A Context is created with
create_context and is its own provider:
call it with a value and children to expose that value to every
descendant, and read it with use_context.
Values live on the reactive ownership tree, so use_context works
anywhere an owner exists: component bodies, effects, memos, and list
rows. The value is handed to consumers exactly as provided, so pass a
signal (or any accessor) when consumers should react to changes.
Example
Classes:
| Name | Description |
|---|---|
ContextNotFoundError |
Raised by |
Context |
A context token that also acts as its provider component. |
Functions:
| Name | Description |
|---|---|
create_context |
Create a |
use_context |
Read the nearest provided value for |
ContextNotFoundError
¶
Bases: LookupError
Raised by use_context when no provider is found and no default exists.
Context
¶
A context token that also acts as its provider component.
Created by create_context. Calling the
context with a value and children returns a provider VNode:
Attributes:
| Name | Type | Description |
|---|---|---|
default |
Value returned by |
|
name |
Optional label for diagnostics. |
create_context
¶
Create a Context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default
|
Any
|
Optional value returned by |
_MISSING
|
name
|
str | None
|
Optional label for diagnostics. |
None
|
Returns:
| Type | Description |
|---|---|
Context[Any]
|
A new |
use_context
¶
use_context(ctx: Context[T]) -> T
Read the nearest provided value for ctx.
Walks up the ownership tree from the active scope and returns the value exactly as the provider received it: a signal or accessor stays a signal or accessor (call it where you need the value), a static value is returned directly.
Raises:
| Type | Description |
|---|---|
ContextNotFoundError
|
No provider is above the caller and the context has no default. |
What's in this module¶
Context passes a value down the tree without threading props. A
Context created by
create_context is its own provider: call
it with a value and children to expose that value to every descendant,
and read it with use_context. Values live on the
ownership tree, so use_context works anywhere an owner exists:
component bodies, effects, memos, and For rows.
| Name | Description |
|---|---|
create_context |
create_context(default=..., *, name=None); without a default, reading outside a provider raises. |
Context |
The token; Theme(value, *children) returns a provider VNode. .default, .name, .has_default. |
use_context |
Nearest provided value, returned exactly as provided (an accessor stays an accessor). |
ContextNotFoundError |
Raised when no provider is above the reader and the context has no default. |
The value is handed to consumers exactly as provided, so pass a signal or accessor when consumers should react to changes, and call it where you need the value.
from wybthon import Accessor, Context, button, component, create_context, create_signal, div, use_context
Theme: Context[Accessor[str]] = create_context(name="Theme")
@component
def ThemedButton():
theme = use_context(Theme) # the accessor the provider passed
return button("Hi", class_=t"btn-{theme}")
@component
def App():
theme, set_theme = create_signal("light")
return Theme( # value first, then children
theme,
div(
ThemedButton(),
button("Toggle", on_click=lambda: set_theme("dark" if theme.peek() == "light" else "light")),
),
)
Nested providers for the same context shadow outer ones. Outside a
component, use_context still works inside any reactive scope
(create_root, an effect, a memo).
See also¶
- Concepts: Context
Owner: where context values are stored- Router:
use_params,use_query, anduse_base_pathare context readers inwybthon.router