Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expanding the documentation #463

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

nate-thegrate
Copy link
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
    • Updated comments for the Hook class to clarify its functionality and usage.
    • Revised descriptions for the createState and shouldRebuild methods to enhance understanding.
    • Clarified the role of HookState and HookElement in managing hook states.
    • Adjusted documentation for HookWidget and StatefulHookWidget to highlight their similarities to standard widgets.
    • Improved clarity on the useContext function's behavior and error handling.

Copy link

coderabbitai bot commented Mar 17, 2025

Walkthrough

The pull request updates documentation within the Flutter Hooks framework. Changes include revisions to the comments for various classes and methods (e.g., use, Hook, HookState, HookElement, HookWidget, StatefulHookWidget, and useContext). The updates clarify how hooks interact with widget build contexts and specify conditions for method calls, thereby providing improved guidance for developers.

Changes

File Path Change Summary
packages/flutter_hooks/.../framework.dart Revised documentation for use, Hook, HookState, HookElement, HookWidget, StatefulHookWidget, and useContext to enhance clarity.

Poem

I hopped through lines of code with cheer,
Finding clarity in docs so clear.
Hooks and widgets now sing in sync,
No more confusion, not even a blink.
A rabbit’s delight with every refined line – pure coding magic, simply divine! 🐇💻


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7b9e7d6 and c953b72.

📒 Files selected for processing (1)
  • packages/flutter_hooks/lib/src/framework.dart (6 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/flutter_hooks/lib/src/framework.dart
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: flutter (flutter_hooks, master)
  • GitHub Check: flutter (flutter_hooks, stable)

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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
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
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 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
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
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 on lines 631 to 633
/// Generally, Hook functions must be called unconditionally, in the same order.
/// That rule does not apply to `useContext()`, however, since instead of accessing a [Hook],
/// it merely returns the relevant [BuildContext].
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can see an argument for calling useContext() unconditionally just for consistency, but since it can make things slightly less readable, I think it's nice to include the information here.

Copy link
Owner

Choose a reason for hiding this comment

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

I'd rather not tell people to do violate hooks rules with useContext specifically.
If we want to allow useContext to not respect those rules, then it shouldn't be named with the useX convention.

IMO diognistic tools could look for use* inside Widget.build and expect all of them to follow hook rules. I'd rather not have exceptions for this ; even if technically doable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IMO diognistic tools could look for use* inside Widget.build and expect all of them to follow hook rules.

That's a good point. I still think it'd be nice for developers who read these docs to have access to this info, but I agree that it shouldn't be recommended. I'll try to adjust the wording here accordingly.

@nate-thegrate
Copy link
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