Back to Biomejs

useQwikMethodUsage

src/content/docs/linter/rules/use-qwik-method-usage.mdx

latest4.1 KB
Original Source

import { Tabs, TabItem } from '@astrojs/starlight/components';

<Tabs> <TabItem label="JSX and TSX" icon="seti:javascript"> ## Summary - Rule available since: `v2.2.6` - Diagnostic Category: [`lint/correctness/useQwikMethodUsage`](/reference/diagnostics#diagnostic-category) - This rule is **recommended**, meaning it is enabled by default. - This rule doesn't have a fix. - The default severity of this rule is [**error**](/reference/diagnostics#error). - This rule belongs to the following domains: - [`qwik`](/linter/domains#qwik) - Sources: - Same as [`qwik/use-method-usage`](https://qwik.dev/docs/advanced/eslint/#use-method-usage)

How to configure

json
{
	"linter": {
		"rules": {
			"correctness": {
				"useQwikMethodUsage": "error"
			}
		}
	}
}

Description

Disallow use* hooks outside of component$ or other use* hooks in Qwik applications.

Ensures Qwik's lifecycle hooks are only used in valid reactive contexts. See Qwik Component Lifecycle for proper hook usage.

Examples

Invalid

js
import { useSignal } from "@builder.io/qwik";

export const Counter = () => {
  const count = useSignal(0);
};
<pre class="language-text"><code class="language-text">code-block.js:4:17 <a href="https://biomejs.dev/linter/rules/use-qwik-method-usage">lint/correctness/useQwikMethodUsage</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">Qwik hook detected outside of an allowed scope.</span> <strong>3 │ </strong>export const Counter = () =&gt; &#123; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>4 │ </strong> const count = useSignal(0); <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>5 │ </strong>&#125;; <strong>6 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Qwik's reactive hooks (functions starting with </span><span style="color: lightgreen;"><strong>use&#42;</strong></span><span style="color: lightgreen;"> followed by uppercase letter) must be: </span> <span style="color: lightgreen;">- Used exclusively within &#96;component$&#96; functions </span> <span style="color: lightgreen;">- Or encapsulated within other valid Qwik hooks</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Check the </span><span style="color: lightgreen;"><a href="https://qwik.dev/docs/components/state/#use-methods">Qwik documentation</a></span><span style="color: lightgreen;">.</span> </code></pre>

Valid

js
import { component$, useSignal } from "@builder.io/qwik";

export const Counter = component$(() => {
  const count = useSignal(0);
});

export const useCounter = () => {
  const count = useSignal(0);
  return count;
};
</TabItem> </Tabs>