By the end of this lesson, you will be able to:
:information_source: What is Data Binding? Data binding in Svelte creates a two-way connection between user input and your application's data. When users type or interact with form elements, the data updates automatically. When you change the data in code, the UI updates instantly!
Data binding makes connecting your UI with data much easier!
Data binding in Svelte gives you several advantages:
In regular JavaScript, you need to create event listeners and manually update values. That's a lot of work!
Svelte makes this super easy. Just add bind:value
to your input tag, and you're done!
:bulb: Tip When you type in the input field, the variable updates instantly. When you change the variable in code, the input field updates too!
App.svelte
<script>
let name = '';
</script>
<input type=text bind:value=\{name\}>
<p>
Hi, \{name\}
</p>
Textareas work just like text inputs! Use the same bind:value
directive:
App.svelte
<script>
let text = "";
</script>
<textarea bind:value=\{text\}></textarea>
<p>You wrote:<br />\{text\}</p>
Numeric inputs use bind:value
too! You can even bind the same variable to both a number input and a range slider:
App.svelte
<script>
let x = 0;
let y = 1;
</script>
<label>
<input type=number bind:value=\{x\} min=0 max=10>
<input type=range bind:value=\{x\} min=0 max=10>
</label>
<label>
<input type=number bind:value=\{y\} min=0 max=10>
<input type=range bind:value=\{y\} min=0 max=10>
</label>
<p>\{x\} + \{y\} = \{x + y\}</p>
:memo: Note Cool Feature Alert! :tada:
Notice how the range slider moves when you type a number? That's because both inputs are bound to the same variable! Also, Svelte automatically calculates
{x + y}
for you - no extra code needed!
For a single checkbox, use bind:checked
instead of bind:value
. This binds to a boolean (true/false) value.
Watch how the value changes from false
to true
when you check the box:
app.svelte
<script>
let isChecked=false;
</script>
Are you a student? <input type=checkbox bind:checked=\{isChecked\}>
<p>
\{isChecked\}
</p>
Output before the checkbox is checked:
Output after the checkbox is checked:
:bulb: Tip Multiple Checkboxes? If you have more than one checkbox working together, use
bind:group
instead (explained below)!
Radio buttons need bind:group
because only one option can be selected at a time. This directive binds all radio buttons together and stores the selected value.
See how the output changes instantly when you select different options:
App.svelte
<script>
let pieces = 1;
</script>
<label>
<input type="radio" bind:group=\{pieces\} value=\{1\}>
One Piece
</label>
<label>
<input type="radio" bind:group=\{pieces\} value=\{2\}>
Two Pieces
</label>
<label>
<input type="radio" bind:group=\{pieces\} value=\{3\}>
Three Pieces
</label>
<p>Selected Piece(s): \{pieces\}</p>
Usually in Svelte, data flows one way: from parent to child. The parent passes data down, but the child can't send data back up.
But wait! With data binding, children can update parent data too! Here's how:
App.svelte (Parent component)
<script>
import Input from './Input.svelte';
let textValue = "";
</script>
<Input bind:prop={textValue} />
<p>
\{textValue\}
</p>
Input.svelte (Child component)
<script>
export let prop;
</script>
<input type="text" placeholder="Type Something" bind:value={prop}>
Let's break down what's happening:
bind:value
to bind the input to its propbind:prop
to create a two-way binding with the childtextValue
updates automatically!:memo: Note Important: The name after
bind:
in the parent must match the exported prop name in the child component.
Here's everything you learned about Svelte data binding:
Input Type | Binding Directive | What It Does |
---|---|---|
Text Input | bind:value |
Binds text input to a string variable |
Textarea | bind:value |
Binds textarea content to a string variable |
Number/Range | bind:value |
Binds numeric input to a number variable |
Single Checkbox | bind:checked |
Binds to a boolean (true/false) value |
Radio Buttons | bind:group |
Binds multiple options to one selected value |
Parent-Child | bind:prop |
Creates two-way binding between components |
bind:prop
Now it's your turn! Try creating:
Remember: The best way to learn is by doing. Open your code editor and start binding!