Performance: Why bindings enable two-way data flow
This affects interaction responsiveness and rendering efficiency by syncing UI and data automatically.
let name = ''; <input bind:value={name} />
let name = ''; <input value={name} on:input={e => name = e.target.value} />
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual input event handling | Multiple event listeners | 1 per input event | Moderate | [!] OK |
| Svelte bind:value directive | Single reactive update | 1 per input event | Low | [OK] Good |
bind: in Svelte enables two-way data flow?bind: does in Sveltebind: directive connects a variable to an input element, so when the user types, the variable updates automatically.name to an input field in Svelte?bind:value={variable}.<input bind:value={name} /> matches the correct syntax. <input bind:name={name} />, <input bind={name} />, and <input value:bind={name} /> use incorrect or invalid syntax. <script>
let count = 0;
</script>
<input type="number" bind:value={count} />
<p>Count is {count}</p>5 in the input?count to the input means typing updates count immediately.{count}, so it updates instantly to show the new value typed. <script>
let text = '';
</script>
<input bind:value={txt} />
<p>You typed: {text}</p>txt, but only text is declared in the script.txt is undefined, input changes don't update anything.txt is not declared in the script. -> Option BisChecked and updates a message below. Which code correctly uses binding for two-way data flow?bind:checked to sync the boolean variable with the checked state.<input type="checkbox" bind:checked={isChecked} /> correctly enables two-way data flow. checked={isChecked} is one-way only. bind={isChecked} lacks the required property name after bind:, making it invalid. bind:isChecked={checked} uses invalid syntax with mismatched variables.Pass the test and this pattern moves unknown