useSvelteReactiveClasses (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.0 - Diagnostic Category:
lint/nursery/useSvelteReactiveClasses - This rule doesn’t have a fix.
- The default severity of this rule is warning.
- This rule belongs to the following domains:
- Sources:
- Inspired from
svelte/prefer-svelte-reactivity
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useSvelteReactiveClasses": "error" } } }}Description
Section titled “Description”Require the reactive classes from svelte/reactivity instead of built-in objects that are
modified.
Svelte updates the page when a $state variable is reassigned, or when a plain object or
array stored in it changes. It doesn’t detect changes made inside objects created from
built-in classes. For example, calling map.set() on a Map, calling date.setMonth()
on a Date, or assigning url.port on a URL changes the object, but the parts of the
page that use it don’t update. Wrapping the object in $state() doesn’t help, because
$state only tracks plain objects and arrays.
The svelte/reactivity module provides versions of these classes that Svelte tracks:
| Built-in class | Svelte class |
|---|---|
Date | SvelteDate |
Map | SvelteMap |
Set | SvelteSet |
URL | SvelteURL |
URLSearchParams | SvelteURLSearchParams |
This rule reports objects created from the built-in classes that are modified after they’re created, including through other variables that hold the same object. Objects that are only read are allowed. Objects stored only in variables declared inside a function are allowed too: each call to the function creates a new object, so the object can’t hold data that the page keeps showing.
In Svelte modules (.svelte.js and .svelte.ts files), the rule also reports objects that
are exported, such as export const cache = new Map(), because other files that import
them can modify them.
Changes made in the component markup, such as in an onclick attribute, aren’t detected.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”<script>const tags = new Set();function addTag(tag) { tags.add(tag);}</script>code-block.svelte:2:14 lint/nursery/useSvelteReactiveClasses ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This Set is modified, but Svelte doesn't detect changes to it.
1 │ <script>
> 2 │ const tags = new Set();
│ ^^^^^^^^^
3 │ function addTag(tag) {
4 │ tags.add(tag);
ℹ It's modified here.
2 │ const tags = new Set();
3 │ function addTag(tag) {
> 4 │ tags.add(tag);
│ ^^^^^^^^^^^^^
5 │ }
6 │ </script>
ℹ Svelte doesn't detect changes made inside a built-in Set, so the parts of the page that use it don't update.
ℹ Use SvelteSet from svelte/reactivity instead.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
<script>const date = $state(new Date());date.setFullYear(2000);</script>code-block.svelte:2:21 lint/nursery/useSvelteReactiveClasses ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This Date is modified, but Svelte doesn't detect changes to it.
1 │ <script>
> 2 │ const date = $state(new Date());
│ ^^^^^^^^^^
3 │ date.setFullYear(2000);
4 │ </script>
ℹ It's modified here.
1 │ <script>
2 │ const date = $state(new Date());
> 3 │ date.setFullYear(2000);
│ ^^^^^^^^^^^^^^^^^^^^^^
4 │ </script>
5 │
ℹ Svelte doesn't detect changes made inside a built-in Date, so the parts of the page that use it don't update.
ℹ Use SvelteDate from svelte/reactivity instead.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
<script>const url = new URL("https://svelte.dev/");url.pathname = "/docs";</script>code-block.svelte:2:13 lint/nursery/useSvelteReactiveClasses ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This URL is modified, but Svelte doesn't detect changes to it.
1 │ <script>
> 2 │ const url = new URL("https://svelte.dev/");
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3 │ url.pathname = "/docs";
4 │ </script>
ℹ It's modified here.
1 │ <script>
2 │ const url = new URL("https://svelte.dev/");
> 3 │ url.pathname = "/docs";
│ ^^^^^^^^^^^^
4 │ </script>
5 │
ℹ Svelte doesn't detect changes made inside a built-in URL, so the parts of the page that use it don't update.
ℹ Use SvelteURL from svelte/reactivity instead.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
<script>import { SvelteSet } from "svelte/reactivity";const tags = new SvelteSet();function addTag(tag) { tags.add(tag);}
const map = new Map([[1, "one"]]);console.log(map.get(1));
function unique(items) { const seen = new Set(); for (const item of items) { seen.add(item); } return [...seen];}</script>See Also
Section titled “See Also”- If you want to remove
$state()around the classes fromsvelte/reactivity, which don’t need it, seenoSvelteUnnecessaryStateWrap.
References
Section titled “References”Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.