Component bindings let you connect data between a parent and child component so they stay in sync automatically.
Component bindings in Svelte
<Child bind:property={parentVariable} />The bind: directive connects a child component's property to a parent variable.
Changes in either the parent or child update the other automatically.
value property of the Input component to the name variable in the parent.<Input bind:value={name} />position property of Slider stays synced with sliderPos in the parent.<Slider bind:position={sliderPos} />checked state of the checkbox is bound to isChecked in the parent.<CustomCheckbox bind:checked={isChecked} />This example shows a parent component with a message variable bound to the text property of the Child component. The child has an input box bound to text. Typing in the input updates message in the parent, and changes to message update the input.
<script> import Child from './Child.svelte'; let message = 'Hello'; </script> <p>Parent message: {message}</p> <Child bind:text={message} /> <!-- Child.svelte --> <script> export let text; </script> <input bind:value={text} aria-label="Child input" />
Use bind: only on properties that the child component explicitly exports.
Bindings keep data in sync both ways without extra code.
Remember to add accessible labels like aria-label for inputs inside child components.
Component bindings connect parent and child data automatically.
Use bind:property to sync values both ways.
This makes UI updates simple and reactive without manual event handling.
Practice
bind:value do in a Svelte component?Solution
Step 1: Understand
In Svelte,bind:valueusagebind:valuelinks a variable to an input's value so changes update both ways.Step 2: Compare options with behavior
Only It creates a two-way connection between a variable and an input's value. describes this two-way syncing correctly; others describe unrelated behaviors.Final Answer:
It creates a two-way connection between a variable and an input's value. -> Option AQuick Check:
Two-way binding = It creates a two-way connection between a variable and an input's value. [OK]
- Thinking bind:value only sets initial value
- Confusing bind:value with event listeners
- Assuming bind:value disables input
count to a child's prop value in Svelte?Solution
Step 1: Recall Svelte binding syntax
To bind a parent's variable to a child's prop, usebind:prop={variable}syntax.Step 2: Evaluate options
<Child bind:value={count} /> matches correct syntax. <Child value={count} /> passes value one-way. Options B and D have invalid syntax.Final Answer:
<Child bind:value={count} /> -> Option DQuick Check:
Correct bind syntax = <Child bind:value={count} /> [OK]
- Using curly braces without bind:
- Trying to bind without ={}
- Passing value as child content instead of prop
<script>
let name = 'Alice';
</script>
<Child bind:name />
<!-- Child.svelte -->
<script>
export let name;
</script>
<input bind:value={name} />What happens when the user types 'Bob' in the input?
Solution
Step 1: Understand two-way binding between parent and child
The parent bindsnameto child'snameprop withbind:name, syncing both variables.Step 2: Input binding updates child's
Typing in input updates child'snameand propagates to parentname, which updates parent'snameautomatically.Final Answer:
The parent'snamevariable updates to 'Bob' automatically. -> Option CQuick Check:
Two-way bind updates parent and child = The parent'snamevariable updates to 'Bob' automatically. [OK]
- Thinking only child updates
- Expecting input to reset on typing
- Believing bind:name is invalid syntax
<script>
let age = 30;
</script>
<Child bind:age></Child>
<!-- Child.svelte -->
<script>
export let age;
</script>
<input value={age} />Solution
Step 1: Check child input binding
The child input usesvalue={age}, which is one-way binding only.Step 2: Fix to two-way binding for sync
To sync changes back to parent, child input must usebind:value={age}.Final Answer:
The child input should usebind:value={age}for two-way binding. -> Option AQuick Check:
Two-way input binding needed = The child input should usebind:value={age}for two-way binding. [OK]
- Using value= instead of bind:value= on inputs
- Thinking export let disables binding
- Misusing bind:age syntax on parent
volume variable as the slider moves. Which approach correctly uses Svelte component bindings to achieve this?Solution
Step 1: Understand binding prop names
The parent binds itsvolumevariable to child'svalueprop usingbind:value={volume}.Step 2: Child exports
The child exportsvalueand binds input's value to itvalueand usesbind:value={value}on the input for two-way sync.Step 3: Check other options for errors
In parent:<Slider value={volume} />
In Slider.svelte:export let value;and<input type='range' value={value} />uses one-way binding only. In parent:<Slider bind:volume={volume} />
In Slider.svelte:export let volume;and<input type='range' bind:value={volume} />mismatches prop names. In parent:<Slider bind:value />
In Slider.svelte:let value;and<input type='range' bind:value={value} />misses binding variable in parent.Final Answer:
Parent binds volume to child's value; child exports value and binds input value. -> Option BQuick Check:
Matching bind names and two-way input binding = In parent:<Slider bind:value={volume} />
In Slider.svelte:export let value;and<input type='range' bind:value={value} />[OK]
- Mismatching prop names between parent and child
- Using one-way binding instead of bind:value
- Not exporting prop in child component
Ready to prove it?
Pass the test and this pattern moves unknown
