Performance: Readonly props
Readonly props affect how data flows and updates between components, impacting rendering speed and user interaction responsiveness.
<script> export let count; // Child treats count as readonly and emits event to parent import { createEventDispatcher } from 'svelte'; const dispatch = createEventDispatcher(); function increment() { dispatch('increment'); } </script> <button on:click={increment}>{count}</button>
<script> export let count; // Child modifies count directly function increment() { count += 1; } </script> <button on:click={increment}>{count}</button>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Child modifies prop directly | Multiple updates | Multiple reflows per update | High paint cost due to frequent changes | [X] Bad |
| Child treats prop as readonly and emits events | Minimal updates | Single reflow per interaction | Low paint cost | [OK] Good |
props are readonly inside a child component?export let to allow parent to pass values.export let count; is valid syntax for a prop declaration. <script>
export let name = 'Guest';
name = 'Alice';
</script>
<p>Hello {name}!</p>name is declared with default 'Guest' but reassigned to 'Alice' inside the component.name causes an error.<script>
export let count = 0;
function increment() {
count += 1;
}
</script>
<button on:click={increment}>Increment</button>
<p>Count: {count}</p>count, which is a prop and readonly inside the component.Pass the test and this pattern moves unknown