MCPcopy Index your code
hub / github.com/reactive-python/reactpy / use_context

Function use_context

src/py/reactpy/reactpy/core/hooks.py:231–249  ·  view source on GitHub ↗

Get the current value for the given context type. See the full :ref:`Use Context` docs for more information.

(context: Context[_Type])

Source from the content-addressed store, hash-verified

229
230
231def use_context(context: Context[_Type]) -> _Type:
232 """Get the current value for the given context type.
233
234 See the full :ref:`Use Context` docs for more information.
235 """
236 hook = current_hook()
237 provider = hook.get_context_provider(context)
238
239 if provider is None:
240 # same assertions but with normal exceptions
241 if not isinstance(context, FunctionType):
242 raise TypeError(f"{context} is not a Context") # nocov
243 if context.__kwdefaults__ is None:
244 raise TypeError(f"{context} has no 'value' kwarg") # nocov
245 if "value" not in context.__kwdefaults__:
246 raise TypeError(f"{context} has no 'value' kwarg") # nocov
247 return cast(_Type, context.__kwdefaults__["value"])
248
249 return provider.value
250
251
252# backend implementations should establish this context at the root of an app

Callers 2

use_connectionFunction · 0.90
use_connectionFunction · 0.85

Calls 2

current_hookFunction · 0.90
get_context_providerMethod · 0.80

Tested by

no test coverage detected