Skip to content

Commit fb4478f

Browse files
committed
add docstrings
1 parent 847277f commit fb4478f

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/py/reactpy/reactpy/core/_life_cycle_hook.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,19 @@ def current_hook() -> LifeCycleHook:
3232

3333

3434
class LifeCycleHook:
35-
"""Defines the life cycle of a layout component.
35+
"""An object which manages the "life cycle" of a layout component.
36+
37+
The "life cycle" of a component is the set of events which occur from the time
38+
a component is first rendered until it is removed from the layout. The life cycle
39+
is ultimately driven by the layout itself, but components can "hook" into those
40+
events to perform actions. Components gain access to their own life cycle hook
41+
by calling :func:`current_hook`. They can then perform actions such as:
42+
43+
1. Adding state via :meth:`use_state`
44+
2. Adding effects via :meth:`add_effect`
45+
3. Setting or getting context providers via
46+
:meth:`LifeCycleHook.set_context_provider` and
47+
:meth:`get_context_provider` respectively.
3648
3749
Components can request access to their own life cycle events and state through hooks
3850
while :class:`~reactpy.core.proto.LayoutType` objects drive drive the life cycle
@@ -137,6 +149,12 @@ def schedule_render(self) -> None:
137149
self._scheduled_render = True
138150

139151
def use_state(self, function: Callable[[], T]) -> T:
152+
"""Add state to this hook
153+
154+
If this hook has not yet rendered, the state is appended to the state tuple.
155+
Otherwise, the state is retrieved from the tuple. This allows state to be
156+
preserved across renders.
157+
"""
140158
if not self._rendered_atleast_once:
141159
# since we're not initialized yet we're just appending state
142160
result = function()
@@ -158,11 +176,21 @@ def add_effect(self, effect_func: EffectFunc) -> None:
158176
self._effect_funcs.append(effect_func)
159177

160178
def set_context_provider(self, provider: ContextProviderType[Any]) -> None:
179+
"""Set a context provider for this hook
180+
181+
The context provider will be used to provide state to any child components
182+
of this hook's component which request a context provider of the same type.
183+
"""
161184
self._context_providers[provider.type] = provider
162185

163186
def get_context_provider(
164187
self, context: Context[T]
165188
) -> ContextProviderType[T] | None:
189+
"""Get a context provider for this hook of the given type
190+
191+
The context provider will have been set by a parent component. If no provider
192+
is found, ``None`` is returned.
193+
"""
166194
return self._context_providers.get(context)
167195

168196
async def affect_component_will_render(self, component: ComponentType) -> None:

0 commit comments

Comments
 (0)