Practice and reinforce the concepts from Lesson 4
Total time: ~60 minutes
:bulb: Tip :bulb: Quick tip: Keep the Svelte REPL open in a separate tab so you can easily switch between instructions and coding!
Time estimate: 15 minutes
Svelte REPL - Start with a clean workspace
Let's create a simple alert button that will trigger a pop-up box when it is clicked.
Steps:
Button.svelte
name
(button text)text
(alert message)colour
(button style class)popUp()
function that shows an alert with the text propon:click
event to the button that calls the popUp function:bulb: Tip :bulb: Event syntax: Use
on:click={functionName}
to handle click events in Svelte. Don't add parentheses after the function name!
Button.svelte:
<script>
export let name;
export let text;
export let colour;
function popUp() \{
alert(text);
\}
</script>
<button class='btn \{colour\} m-1' on:click=\{popUp\}>
\{name\}
</button>
<style>
@import url("https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css");
</style>
App.svelte:
<script>
import Button from "./Button.svelte";
let btn1 = \{
name: 'Button',
text: 'Hello',
colour: 'btn-success'
\};
let btn2 = \{
name: 'Alert',
text: 'Alert',
colour: 'btn-primary'
\};
</script>
<Button \{...btn1\} />
<Button \{...btn2\} />
If you've done it correctly, it should look like this:
Time estimate: 20 minutes
Svelte REPL - Start fresh for this exercise
Let's create a PIN lock system that prompts users for a PIN and validates it.
Steps:
btnName
- for the button textmessage
- for the status messagecheck()
function that:
on:click
event to call the check function:bulb: Tip :bulb: Number conversion: Use
+prompt()
to convert the string input to a number. This is important for comparing with the PIN number!
:warning: Warning :warning: Common mistake: Make sure to use
!==
for comparison, not!=
, to ensure strict type checking.
App.svelte:
<script>
let btnName = "Unlock";
let message="Click on the button to open the locker.";
function check() \{
let pinNo = +prompt("Please enter the PIN number:");
if (pinNo !== 4321) \{
message = "Wrong pin number!";
\} else \{
btnName = "Opened";
message = "Successfully opened the locker!";
\}
\}
</script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<button on:click=\{check\} class="btn btn-success">
\{btnName\}
</button>
<p class="mt-2">\{message\}</p>
Before pressing on the Unlock button:
After pressing on the Unlock button:
Correct pin number:
Wrong pin number:
Time estimate: 15 minutes
Svelte REPL - Start with a new workspace
Let's create a page that can update and display your information.
Steps:
name
, age
, githubId
, phoneNumber
(all empty strings)update()
function that:
prompt()
to ask for each piece of informationon:click
event:bulb: Tip :bulb: Reactive updates: In Svelte, when you change a variable's value, the UI automatically updates. No need to manually refresh!
App.svelte:
<script>
let name = '';
let age = '';
let githubId = '';
let phoneNumber = '';
function update() \{
name = prompt('Please enter your name');
age = prompt('Please enter your age');
githubId = prompt('Please enter your Github ID');
phoneNumber = prompt('Please enter your phone number');
\}
</script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<h1 class="text-center text-white bg-dark mb-0 p-2">
Exercise 3
</h1>
<div class='bg-secondary p-5 text-center text-white'>
<p>
Name: \{name\}
</p>
<p>
Age: \{age\}
</p>
<p>
Github ID: \{githubId\}
</p>
<p>
Phone Number: \{phoneNumber\}
</p>
<button class='btn btn-success mr-2' on:click=\{update\}>
Update
</button>
</div>
Time estimate: 10 minutes
Continuing from Activity 3, let's add a clear button to reset all the information.
Steps:
clear()
function that:
""
)btn-danger
class for visual distinctionon:click
event to call the clear function:bulb: Tip :bulb: Button spacing: Use Bootstrap's
mr-2
class on the first button to add space between buttons!
Enhanced App.svelte:
<script>
let name = '';
let age = '';
let githubId = '';
let phoneNumber = '';
function update() \{
name = prompt('Please enter your name');
age = prompt('Please enter your age');
githubId = prompt('Please enter your Github ID');
phoneNumber = prompt('Please enter your phone number');
\}
function clear() \{
name = "";
age = "";
githubId = "";
phoneNumber = "";
\}
</script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<h1 class="text-center text-white bg-dark mb-0 p-2">
Exercise 3
</h1>
<div class='bg-secondary p-5 text-center text-white'>
<p>
Name: \{name\}
</p>
<p>
Age: \{age\}
</p>
<p>
Github ID: \{githubId\}
</p>
<p>
Phone Number: \{phoneNumber\}
</p>
<button class='btn btn-success mr-2' on:click=\{update\}>
Update
</button>
<button class='btn btn-danger' on:click=\{clear\}>
Clear
</button>
</div>
Make sure your Svelte Events projects include:
on:click
event syntaxCommon issues and solutions:
"Function is not defined" error
<script>
tagClick event not working
on:click={functionName}
(no parentheses!)Props not showing
export let propName;
Alert/Prompt not appearing
:information_source: Info :memo: Ready to submit? Make sure to test all event handlers and verify they work correctly in the Svelte REPL before submitting!
Submission checklist:
- All 4 activities completed
- Click events working properly
- Functions executing correctly
- Props being passed and displayed
- Tested in Svelte REPL
Code Review: YouTube Tutorial