edifice.provide_context#
- edifice.provide_context(context_key, initial_state)[source]#
Context state provider Hook for prop drilling.
Provides similar features to React useContext.
- Parameters:
context_key (
str
) – Identifier for a shared context.initial_state (
Union
[TypeVar
(_T_provide_context
),Callable
[[],TypeVar
(_T_provide_context
)]]) – The initial state value or initializer function.
- Return type:
tuple
[TypeVar
(_T_provide_context
),Callable
[[Union
[TypeVar
(_T_provide_context
),Callable
[[TypeVar
(_T_provide_context
)],TypeVar
(_T_provide_context
)]]],None
]]- Returns:
A tuple pair containing
The current state value for the given
context_key
.A setter function for setting or updating the state value.
Use this Hook to transmit state without passing the state down through the props to a child
@component
usinguse_context()
.provide_context()
is called with acontext_key
and an initial value. It returns a state value and a setter function. The initial value, state value and setter function behave exactly like the ones documented inuse_state()
.The
context_key
must be unique for eachprovide_context()
Hook.The setter function will, when called, update the state value across each
@component
usinguse_context()
with the samecontext_key
.@component def ContextChild(self): x, x_setter = use_context("x_context_key", int) Button( title=str(x) + "+1" on_click = lambda _event: x_setter(x + 1) ) @component def ContextParent(self): x, _x_setter = provide_context("x_context_key", 0) with VBoxview(): Label(text=str(x)) ContextChild() ContextChild()