@@ -17,13 +17,19 @@ bool debugHotReloadHooksEnabled = true;
1717// ignore: deprecated_member_use, deprecated_member_use_from_same_package
1818R use <R >(Hook <R > hook) => Hook .use (hook);
1919
20- /// [Hook] is similar to a [StatelessWidget] , but is not associated
21- /// to an [Element ] .
20+ /// Allows a [Widget] to create and access its own mutable data
21+ /// without implementing a [State ] .
2222///
23- /// A [Hook] is typically the equivalent of [State] for [StatefulWidget] ,
24- /// with the notable difference that a [HookWidget] can have more than one [Hook] .
25- /// A [Hook] is created within the [HookState.build] method of a [HookWidget] and the creation
26- /// must be made unconditionally, always in the same order.
23+ /// Whereas [Widget] s store the immutable configuration for UI components,
24+ /// [Hook] s store immutable configuration for any type of object.
25+ /// The [HookState] of a [Hook] is analogous to the [State] of a [StatefulWidget] ,
26+ /// and a single [HookWidget] can use more than one [Hook] .
27+ ///
28+ /// Hooks can be used by replacing `extends StatelessWidget` with `extends HookWidget` ,
29+ /// or by replacing `Builder()` with `HookBuilder()` .
30+ ///
31+ /// Hook functions must be called unconditionally during the `build()` method,
32+ /// and always in the same order.
2733///
2834/// ### Good:
2935/// ```
@@ -216,7 +222,11 @@ Calling them outside of build method leads to an unstable state and is therefore
216222 HookState <R , Hook <R >> createState ();
217223}
218224
219- /// The logic and internal state for a [HookWidget]
225+ /// The logic and internal state of a [Hook] .
226+ ///
227+ /// This class is similar to a [State] , but instead of building a [Widget]
228+ /// subtree, the [build] method can return a value of any type, as specified
229+ /// by the "result" type argument `R` .
220230abstract class HookState <R , T extends Hook <R >> with Diagnosticable {
221231 /// Equivalent of [State.context] for [HookState]
222232 @protected
@@ -277,22 +287,19 @@ abstract class HookState<R, T extends Hook<R>> with Diagnosticable {
277287
278288 /// Called before a [build] triggered by [markMayNeedRebuild] .
279289 ///
280- /// If [shouldRebuild] returns `false` on all the hooks that called [markMayNeedRebuild]
281- /// then this aborts the rebuild of the associated [HookWidget ] .
290+ /// If [shouldRebuild] returns `false` on all the hooks that called [markMayNeedRebuild] ,
291+ /// [HookElement.build] will return a cached value instead of rebuilding each [Hook ] .
282292 ///
283- /// There is no guarantee that this method will be called after [markMayNeedRebuild]
284- /// was called.
285- /// Some situations where [shouldRebuild] will not be called:
286- ///
287- /// - [setState] was called
288- /// - a previous hook's [shouldRebuild] returned `true`
289- /// - the associated [HookWidget] changed.
293+ /// This method is not evaluated if a previous Hook called [markMayNeedRebuild]
294+ /// and its [shouldRebuild] method returned `true` .
295+ /// Additionally, if [setState] , [didUpdateHook] , or [HookElement.didChangeDependencies] is called,
296+ /// the build is unconditional and the `shouldRebuild()` call is skipped.
290297 bool shouldRebuild () => true ;
291298
292299 /// Mark the associated [HookWidget] as **potentially** needing to rebuild.
293300 ///
294301 /// As opposed to [setState] , the rebuild is optional and can be cancelled right
295- /// before ` build` is called, by having [shouldRebuild] return false.
302+ /// before [ build] is called, by having [shouldRebuild] return false.
296303 void markMayNeedRebuild () {
297304 if (_element! ._isOptionalRebuild != false ) {
298305 _element!
@@ -368,7 +375,10 @@ extension on HookElement {
368375 }
369376}
370377
371- /// An [Element] that uses a [HookWidget] as its configuration.
378+ /// An [Element] that manages [Hook] s by storing the associated [HookState] s in a [LinkedList] .
379+ ///
380+ /// [use] and [useContext] can only be called during this element's [build] method.
381+ /// The `_buildCache` enables the behavior described in [HookState.shouldRebuild] .
372382mixin HookElement on ComponentElement {
373383 static HookElement ? _currentHookElement;
374384
@@ -576,14 +586,12 @@ Type mismatch between hooks:
576586 }
577587}
578588
579- /// A [Widget] that can use a [Hook] .
589+ /// A [Widget] that can use [Hook] s .
580590///
581- /// Its usage is very similar to [StatelessWidget] .
582- /// [HookWidget] does not have any life cycle and only implements
583- /// the [build] method.
591+ /// Similar to [StatelessWidget] , a `HookWidget` is created by extending this class.
584592///
585- /// The difference is that it can use a [ Hook] , which allows a
586- /// [HookWidget] to store mutable data without implementing a [State] .
593+ /// The [HookWidget.build] method can [ use] Hook functions, allowing this widget
594+ /// to store mutable data without implementing a [State] .
587595abstract class HookWidget extends StatelessWidget {
588596 /// Initializes [key] for subclasses.
589597 const HookWidget ({Key ? key}) : super (key: key);
@@ -596,12 +604,13 @@ class _StatelessHookElement extends StatelessElement with HookElement {
596604 _StatelessHookElement (HookWidget hooks) : super (hooks);
597605}
598606
599- /// A [StatefulWidget] that can use a [Hook] .
607+ /// A [StatefulWidget] that can use [Hook] s .
600608///
601- /// Its usage is very similar to that of [StatefulWidget] , but uses hooks inside [State.build] .
609+ /// Similar to [StatefulWidget] , this widget creates a [State] which
610+ /// can store mutable data.
602611///
603- /// The difference is that it can use a [Hook] , which allows a
604- /// [HookWidget] to store mutable data without implementing a [State ] .
612+ /// The difference is that [Hook] functions can be called from within [State.build] ,
613+ /// similar to [HookWidget.build ] .
605614abstract class StatefulHookWidget extends StatefulWidget {
606615 /// Initializes [key] for subclasses.
607616 const StatefulHookWidget ({Key ? key}) : super (key: key);
0 commit comments