Performance: Component bindings
Component bindings affect how quickly the UI updates in response to user input or data changes, impacting interaction responsiveness.
<Child value={parentValue} on:valueChange={e => parentValue = e.detail} />
<script>
let parentValue = '';
</script><Child bind:value={parentValue} />
<script>
let parentValue = '';
</script>| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Two-way bind:value | High (updates on every input) | Multiple per keystroke | High (many repaints) | [X] Bad |
| One-way value + event handler | Lower (updates only on change) | Few | Lower | [OK] Good |
bind:value do in a Svelte component?bind:value usagebind:value links a variable to an input's value so changes update both ways.count to a child's prop value in Svelte?bind:prop={variable} syntax.<script>
let name = 'Alice';
</script>
<Child bind:name />
<!-- Child.svelte -->
<script>
export let name;
</script>
<input bind:value={name} />name to child's name prop with bind:name, syncing both variables.name and propagates to parentname, which updates parent's name automatically.name variable updates to 'Bob' automatically. -> Option Cname variable updates to 'Bob' automatically. [OK]<script>
let age = 30;
</script>
<Child bind:age></Child>
<!-- Child.svelte -->
<script>
export let age;
</script>
<input value={age} />value={age}, which is one-way binding only.bind:value={age}.bind:value={age} for two-way binding. -> Option Abind:value={age} for two-way binding. [OK]volume variable as the slider moves. Which approach correctly uses Svelte component bindings to achieve this?volume variable to child's value prop using bind:value={volume}.value and binds input's value to itvalue and uses bind:value={value} on the input for two-way sync.<Slider value={volume} />export let value; and <input type='range' value={value} /> uses one-way binding only. In parent: <Slider bind:volume={volume} />export let volume; and <input type='range' bind:value={volume} /> mismatches prop names. In parent: <Slider bind:value />let value; and <input type='range' bind:value={value} /> misses binding variable in parent.<Slider bind:value={volume} />export let value; and <input type='range' bind:value={value} /> [OK]Pass the test and this pattern moves unknown