Practice and reinforce the concepts from Lesson 6
In this hands-on activity, you'll master:
bind:
directiveTotal Time: 60-75 minutes
:bulb: Tip Save your work frequently! The Svelte REPL can lose changes if you refresh the page accidentally.
Time: 10-15 minutes
Create a real-time name display that updates as you type and shows a welcome message.
Steps:
name
in the script tag (don't assign any value)<input>
tag with type="text"
and bind it to the name variablepopUp()
function that alerts "Welcome " + name<h1>
tag:bulb: Tip Use
bind:value={variableName}
for two-way data binding. This creates a live connection between the input and your variable!
<script>
let name;
function popUp() \{
alert('Welcome ' + name);
\}
</script>
<input type="text" bind:value=\{name\}>
<button on:click=\{popUp\}>OK</button>
<h1>My name is \{name\}!</h1>
Time: 15-20 minutes
Build a calculator that divides two numbers with automatic validation to ensure both numbers are between 0-20.
Steps:
number1
and number2
, both assigned to 0<p>
tags with "First Number" and "Second Number" labels<input>
tags with type="number"
and bind them to the variables:bulb: Tip Use
$:
for reactive statements that run automatically when variables change. This is perfect for validation!
<script>
let number1 = 0;
let number2 = 0;
$: if(number1 < 0 || number1 > 20) \{
alert("Please enter a value between 0-20");
number1 = 0;
\}
$: if(number2 < 0 || number2 > 20) \{
alert("Please enter a value between 0-20");
number2 = 0;
\}
</script>
<p>First Number</p>
<input type="number" bind:value=\{number1\}>
<p>Second Number</p>
<input type="number" bind:value=\{number2\}>
<p>\{number1\} ÷ \{number2\} = \{number1 / number2\}</p>
When the number range is between 0-20:
When the number range is not in between 0-20:
Time: 20-25 minutes
Create an interactive ordering system for an ice-cream shop where customers can enter their name and choose flavors.
Steps:
Order.svelte
(click the + button)Order.svelte
, export props cName
and flavor
(no initial values)cName
and radio buttons bound to flavor
App.svelte
, import the Order component and bind the props:bulb: Tip Use
bind:group={variableName}
for radio button binding. This automatically manages which option is selected!
Order.svelte:
<script>
export let cName;
export let flavor;
</script>
<h1>Welcome to Telebort Ice-cream Shop!</h1>
<p>Enter your name:</p>
<input type="text" bind:value=\{cName\}>
<p>Select the flavour that you want:</p>
<input type="radio" bind:group=\{flavor\} value="vanilla"> Vanilla <br />
<input type="radio" bind:group=\{flavor\} value="chocolate"> Chocolate <br />
<input type="radio" bind:group=\{flavor\} value="choco vanilla"> Choco Vanilla <br />
<p>Hi, \{cName\}. You wanted the \{flavor\} ice-cream.</p>
App.svelte:
<script>
import Order from "./Order.svelte";
let cName = "";
let flavor = "";
</script>
<Order bind:cName=\{cName\} bind:flavor=\{flavor\} />
Time: 15-20 minutes
Take your ice-cream shop to the next level with improved styling and user experience!
Enhancement Ideas:
:bulb: Tip Use Svelte's style blocks to add CSS directly in your components. Remember, styles are scoped by default!
Make sure your Svelte Data Binding projects include:
:white_check_mark: Activity 1: Text input with two-way binding and real-time display updates :white_check_mark: Activity 2: Number inputs with reactive validation and range checking :white_check_mark: Activity 3: Component communication using bound props and radio button groups :white_check_mark: Activity 4: Enhanced styling and user experience improvements
All projects must demonstrate:
bind:
directive syntaxBinding Type | Syntax | Use Case |
---|---|---|
Text/Number Input | bind:value={variable} |
Two-way data binding |
Radio Buttons | bind:group={variable} |
Group selection |
Component Props | bind:propName={variable} |
Parent-child communication |
Reactive Statements | $: statement |
Auto-run on changes |
:bulb: Tip Remember: The division symbol can be typed as
÷
or use the HTML entity÷
Common Issues:
Binding not working?
{variable}
not variable
Radio buttons not grouping?
bind:group
variablevalue
attributeValidation not triggering?
$:
for reactive statementsComponent props not updating?
bind:
when passing props from parent to child:warning: Warning Important: Test all functionality before submitting!
- :emoji: All bindings work correctly
- :emoji: Validation triggers properly
- :emoji: Components communicate as expected
When you have completed all activities and tested your Svelte Data Binding projects:
:information_source: Info :bulb: Submission Tip: Keep your REPL tabs open until you receive confirmation that your submission was received.