Svelte

svelte/props-derived

A top-level value computed from props is a $derived; a plain let or const keeps only the first value.

umiarkowanastatycznefe-svelte

Dlaczego

The script of a Svelte 5 component runs once. const label = size === 'small' ? 'S' : 'L' reads the prop at creation and never again, so when the parent changes size the label stays wrong. The compiler warns about it (state_referenced_locally), but the warning is easy to miss in a long build log.

Poprawka

Wrap the expression in $derived(...) (or $derived.by for a block). If you really want the first value only, say so: let initial = $state(untrack(() => value)) or name it initialValue.

Przykłady

<!-- bad -->
<script>
  let { size, price, locale } = $props()
  const label = size === 'small' ? 'S' : 'L'
  const formatter = new Intl.NumberFormat(locale, { style: 'currency', currency: 'EUR' })
</script>
<!-- good -->
<script>
  let { size, price, locale } = $props()
  const label = $derived(size === 'small' ? 'S' : 'L')
  const formatter = $derived(new Intl.NumberFormat(locale, { style: 'currency', currency: 'EUR' }))
</script>

Źródła

Fakty

Identyfikator
svelte/props-derived
Waga
umiarkowana
Sprawdzanie
statyczne sprawdzane w kodzie źródłowym przez audit
Dotyczy plików
svelte
Tylko w projektach z
svelte sveltekit
Uczy jej skill
fe-svelte Svelte components
Od wersji
0.1.0

Plik reguły na GitHubie

Przeczytaj w terminalu

npx github:dawidolko/frontend-skills explain svelte/props-derived