@@ -32,7 +32,19 @@ def current_hook() -> LifeCycleHook:
32
32
33
33
34
34
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.
36
48
37
49
Components can request access to their own life cycle events and state through hooks
38
50
while :class:`~reactpy.core.proto.LayoutType` objects drive drive the life cycle
@@ -137,6 +149,12 @@ def schedule_render(self) -> None:
137
149
self ._scheduled_render = True
138
150
139
151
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
+ """
140
158
if not self ._rendered_atleast_once :
141
159
# since we're not initialized yet we're just appending state
142
160
result = function ()
@@ -158,11 +176,21 @@ def add_effect(self, effect_func: EffectFunc) -> None:
158
176
self ._effect_funcs .append (effect_func )
159
177
160
178
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
+ """
161
184
self ._context_providers [provider .type ] = provider
162
185
163
186
def get_context_provider (
164
187
self , context : Context [T ]
165
188
) -> 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
+ """
166
194
return self ._context_providers .get (context )
167
195
168
196
async def affect_component_will_render (self , component : ComponentType ) -> None :
0 commit comments