From 27e7757503fd9ae22da1dd2dca107c9cb00dc594 Mon Sep 17 00:00:00 2001 From: nikaera Date: Mon, 4 Mar 2024 11:04:16 +0900 Subject: [PATCH] docs: Added the rules for hooks_avoid_within_class. --- README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/README.md b/README.md index 2c69802..f650507 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ The currently available lint rules are as follows: | LintRule | Description | Quickfix | | ------------- | ------------- | ------------- | | [hooks_avoid_nesting](#hooks_avoid_nesting) | You should use Hooks only inside the build method of a Widget. | | +| [hooks_avoid_within_class](#hooks_avoid_within_class) | Hooks must not be defined within the class. | | | [hooks_name_convention](#hooks_name_convention) | DO always prefix your hooks with use, https://pub.dev/packages/flutter_hooks#rules. | | | [hooks_extends](#hooks_extends) | Using Hooks inside a Widget other than HookWidget or HookConsumerWidget will result in an error at runtime. | ✅ | | [hooks_unuse_widget](#hooks_unuse_widget) | If you are not using Hooks inside of a Widget, you do not need HookWidget or HookConsumerWidget. | ✅ | @@ -118,6 +119,30 @@ Widget build(BuildContext context) { } ``` +### hooks_avoid_within_class + +Defining Custom Hooks within a class mixes the characteristics of class and method, leading to potentially complex code. + +**Bad** + +```dart +class TestHelper { + const TestHelper._(); + + static void useEffectOnce(Dispose? Function() effect) { // ❌ + useEffect(effect, const []); + } +} +``` + +**Good**: + +```dart +void useEffectOnce(Dispose? Function() effect) { // ⭕ + useEffect(effect, const []); +} +``` + ### hooks_name_convention **DO always prefix your hooks with use.**