Skip to content

Expanding the documentation#463

Open
nate-thegrate wants to merge 1 commit intorrousselGit:masterfrom
nate-thegrate:documentation
Open

Expanding the documentation#463
nate-thegrate wants to merge 1 commit intorrousselGit:masterfrom
nate-thegrate:documentation

Conversation

@nate-thegrate
Copy link
Copy Markdown
Contributor

@nate-thegrate nate-thegrate commented Mar 17, 2025

I've been doing things with Hooks a lot over the past few months, and it's been really fun to learn about the inner mechanisms of flutter_hooks.

The goal of this PR is to flesh out a few of the API descriptions and to tweak the wording in a few places for better accuracy.

Summary by CodeRabbit

  • Documentation
    • Clarified Hook usage: how widgets use hooks to manage mutable data and that hooks must be called unconditionally and in a fixed order during build.
    • Explained when rebuild checks are skipped and how cached hook values are used to avoid unnecessary rebuilds.
    • Clarified the roles and lifecycle of HookState, HookElement, HookWidget, and StatefulHookWidget.
    • Improved wording around context access and proper usage within build.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 17, 2025

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 393ffd4b-db7e-4995-b3fc-1f0125b7ee44

📥 Commits

Reviewing files that changed from the base of the PR and between c953b72 and 4c2f83c.

📒 Files selected for processing (1)
  • packages/flutter_hooks/lib/src/framework.dart
✅ Files skipped from review due to trivial changes (1)
  • packages/flutter_hooks/lib/src/framework.dart

📝 Walkthrough

Walkthrough

Documentation in packages/flutter_hooks/lib/src/framework.dart was revised to clarify the semantics and relationships of Hook, HookState, HookElement, HookWidget, StatefulHookWidget, and use/useContext, including when hook functions must be called and when shouldRebuild is consulted or skipped during build cycles.

Changes

Cohort / File(s) Summary
Framework docs
packages/flutter_hooks/lib/src/framework.dart
Rewrote docblocks to: redefine Hook vs widget/state responsibilities; clarify HookStateState analogy; state that HookWidget/HookBuilder/StatefulHookWidget run hook functions and require unconditional, fixed-order calls in build(); detail HookElement storage of HookStates and when shouldRebuild() is bypassed.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

I nibbled at comments, lines in a row,
Tuned hooks to their places so builds clearly flow.
Widgets and states now each know their part,
I hopped through the docs with a joyful heart.
Rabbit-approved fixes—small, precise, smart. 🐇

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Title check ❓ Inconclusive The title 'Expanding the documentation' is generic and vague, using non-descriptive terms that don't convey what specific documentation was expanded or improved. Consider a more specific title like 'Improve framework.dart documentation for hooks' or 'Clarify Hook, HookState, and HookElement documentation' to better communicate the scope of documentation changes.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Comment thread packages/flutter_hooks/lib/src/framework.dart Outdated
Comment on lines -20 to +22
/// [Hook] is similar to a [StatelessWidget], but is not associated
/// to an [Element].
/// Allows a [Widget] to create and access its own mutable data
/// without implementing a [State].
Copy link
Copy Markdown
Contributor Author

@nate-thegrate nate-thegrate Mar 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that "not associated to an Element" might lead to confusion, because much like a StatefulWidget, Hook has a createState() method, and the object returned has access to the context.

Comment on lines -23 to +33
/// A [Hook] is typically the equivalent of [State] for [StatefulWidget],
/// with the notable difference that a [HookWidget] can have more than one [Hook].
/// A [Hook] is created within the [HookState.build] method of a [HookWidget] and the creation
/// must be made unconditionally, always in the same order.
/// Whereas [Widget]s store the immutable configuration for UI components,
/// [Hook]s store immutable configuration for any type of object.
/// The [HookState] of a [Hook] is analogous to the [State] of a [StatefulWidget],
/// and a single [HookWidget] can use more than one [Hook].
///
/// Hooks can be used by replacing `extends StatelessWidget` with `extends HookWidget`,
/// or by replacing `Builder()` with `HookBuilder()`.
///
/// Hook functions must be called unconditionally during the `build()` method,
/// and always in the same order.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A [Hook] is typically the equivalent of [State]

This makes sense, since you can go by the philosophy of "rather than making a State, make a Hook instead".
However, in my mind, the equivalent of State would be HookState rather than Hook.
My hope is that the adjusted wording creates a good intuition while avoiding this caveat.


A [Hook] is created within the [HookState.build] method of a [HookWidget]

I believe this sentence was a typo: a HookState is created by a Hook, not the other way around.

Comment thread packages/flutter_hooks/lib/src/framework.dart Outdated
Comment on lines 289 to 298
/// Called before a [build] triggered by [markMayNeedRebuild].
///
/// If [shouldRebuild] returns `false` on all the hooks that called [markMayNeedRebuild]
/// then this aborts the rebuild of the associated [HookWidget].
///
/// There is no guarantee that this method will be called after [markMayNeedRebuild]
/// was called.
/// Some situations where [shouldRebuild] will not be called:
/// If [shouldRebuild] returns `false` on all the hooks that called [markMayNeedRebuild],
/// [HookElement.build] will return a cached value instead of rebuilding each [Hook].
///
/// - [setState] was called
/// - a previous hook's [shouldRebuild] returned `true`
/// - the associated [HookWidget] changed.
/// This method is not evaluated if a previous Hook called [markMayNeedRebuild]
/// and its [shouldRebuild] method returned `true`.
/// Additionally, if [setState], [didUpdateHook], or [HookElement.didChangeDependencies] is called,
/// the build is unconditional and the `shouldRebuild()` call is skipped.
bool shouldRebuild() => true;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the original description was really good, but I saw that (1) a rebuild will happen anyway if "a previous hook's shouldRebuild returned true" and (2) the method returns true by default… and for some reason I totally missed that only the hooks that called markMayNeedRebuild are checked.

I also saw that the 3 bullet points technically didn't cover every possibility, since an InheritedWidget could also trigger an unconditional rebuild.

Comment on lines -579 to +590
/// A [Widget] that can use a [Hook].
/// A [Widget] that can use [Hook]s.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My hope was to tweak the wording here, so it's more explicit that you can use multiple Hooks.

Comment thread packages/flutter_hooks/lib/src/framework.dart Outdated
Comment thread packages/flutter_hooks/lib/src/framework.dart Outdated
@nate-thegrate
Copy link
Copy Markdown
Contributor Author

Quick update—I've reverted the changes to the useContext description.

Hopefully now we can just focus on the uncontroversial changes here. I especially like the implementation of markMayNeedRebuild; I'd be happy if future devs had an easier time taking advantage of it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants