Svelte
svelte/props-derived
A top-level value computed from props is a $derived; a plain let or const keeps only the first value.
moderatestaticfe-svelte
Why
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.
Fix
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.
Examples
<!-- 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>Sources
Facts
- Id
svelte/props-derived- Severity
- moderate
- Check
- static checked in the source by
audit - Applies to
svelte- Only in projects with
sveltesveltekit- Taught by the skill
fe-svelteSvelte components- Since version
- 0.1.0
Read it in the terminal
npx github:dawidolko/frontend-skills explain svelte/props-derived