Svelte Tutorial for Beginners #7 - Inline Event Handlers
Key Takeaways
Covers inline event handlers in Svelte for handling user interactions
Full Transcript
or rather than gang so in the last video we cycled through this data and we output a snippet of HTML for each person in the array using the each loop and we can see that over here everything looks fine now imagine I want to place a button on to each person and when we click on that button it deletes that person from the array of data over here and therefore when that happens we update this data it should reflect that change in the browser and re-render this list well we can do that let me first of all add the button down here and it's a button not a button and we'll say delete inside that and we need to add on a click handler to this button right because we're going to react to a user clicking it and inside here will reference a function so for example handle click but you can call the function whatever you want and then we need to create that function up here Const handle click is equal to a function like so and then in here this is where we want to delete the person from people right now how do we do this in here because right now we don't know which person to delete because we're not passing through this person into the function right here and we can't access the person over here I can demo that if I see console dot log person like so we're not going to be able to access that and if i refresh and delete something we see person is not defined so just because we're referencing a function inside this loop where we have access to person over here doesn't mean we have access to it in the handler function itself so instead what we need to do is figure out a way to pass data through into this function as a parameter or argument rather so for example you could pass through the person right here or even person ID and then we could access the ID in here but there is a problem with this and that is that we're now invoking this function automatically so as we first run this code and we output this template for each person in the array it's going to automatically invoke this function for every single person without was clicking it right because we're invoking it using these parentheses so that means it's gonna go ahead invoke this function and if we have code in here to delete that person from the array it's gonna automatically delete them straight away and we don't want that let me just demo this in action a little bit I'm going to log out the ID to the console and if I save this now we should automatically see one two three so it's running this function for each person in the array automatically and we don't want that to happen so what we could do instead is cut this one out and we could create an inline function right here let me just delete that closing button it's automatically added that in for me when I don't need it but now we have this inline function right here right and we're not invoking this function and I can demo that I can say console dot log and then say clicked me right so this is only going to now run when we click on the button because this is not being invoked we don't have parentheses on the end of it over here so it's just a reference to a function which runs when we click it now we don't get anything logged here but if I click on delete you see clicked me clicked me etc so this works but we don't just want to log that to the console instead inside here we now want to invoke this function and we can do that by saying handle click and we can pass through the person dots ID so now this is fine because now this function is wrapping this function so this is not getting automatically invoked because it sits inside a function which is not yet invoked until we click on this so now that would work let me just save this and demo it so we don't get them automatically but when we click on delete now we can see the ID of that ninja so we're now passing it through into this function and we can use that ID to go ahead and delete the person from the array now before we do that let me just get rid of this curly brace here to open up the code block of the function and this one because when we use an our function if we're just doing it all in one line like this we don't need that curly brace and closing curly brace so now let's get rid of this console lockdown here we don't need that but what we do need to do now is update this people array right here so now I could say people is equal to people so what the current value is dots filter so this is just a regular JavaScript method and it filters through the array so we can remove certain items out so what this does is take a callback function right here so let me just create this function and this function then takes in each person as we cycle through it right so the filter method cycles through the array and fires a callback function for each person and we take that person in right here so and by the way you can call this what you want you can call it a or b or whatever you want it makes sense to me to call it person and then inside here we return true or false if we return true then it keeps that person in the array if we return false then it removes that person from the array so if I now say well we'll take the person and get the ID and we'll check if that is not equal to the ID because if they're not equal then we want to keep that person in the array remember we pass through the ID of the person that we click on right here so say we pass through two it means we've clicked on this one right so we filter the array we fire a callback function for each item it checks if the ID that we clicked on is not equal to the ID we pass through if they're not equal then it will return true and we'll keep that in and that's right now if they are equal this will return false therefore it will filter that item out of the array does that make sense cool so this right here returns a new array without the filtered item and then what we're doing is updating the value of people with that filtered array we have to reassign because spelt looks for this reassignment the equal sign if you like to check that something's been updated we can't just do this right here because that is not actually going to work we're not reassigning any kind of data instead we need to explicitly reassign the data based on the results of this and then when svelt sees this reassignment and takes the value of people then it reruns to output the updated list down here so if we save this cross our fingers and test this let's try deleting Mario and boom it goes Yoshi Luigi and now there are no people to show so there we go my friends that's how we can pass through a argument to a click handler or any other kind of handler function where an event occurs we wrap it inside an inline function which is not automatically invoked when the code runs now one more thing I said that we can take in and events objects any handler when we register it right we did that before and this is no different but this time the event object doesn't go directly in here because this is now no longer the direct handler this is just a function we're invoking inside the handler which is this now so we take in the event object right here and then if you wanted to you could pass it in as an argument into this handle click function we could take it in here then as a parameter and if we wanted to use it we could so console.log e just to see that this works save it and delete and now we can see we get this event object with information about the events right here we don't need it in our case so I'm gonna get rid of it again but if you want to use it that is how you would use it pass it through to the inline function and then into your custom handler if you need it okay so that is inline functions in the next video we're going to take a look at conditionals in our template
Original Description
Hey all, in this Svelte tutorial we'll talk a little bit about inline event handlers (for things like click events) and why we'd use them.
🐱👤🐱👤 JOIN THE GANG -
https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join
----------------------------------------
🐱💻 🐱💻 My Udemy Courses:
+ Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript
+ Vue JS & Firebase - http://www.thenetninja.co.uk/udemy/vue-and-firebase
+ D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase
🐱💻 🐱💻 Course Files:
https://github.com/iamshaunjp/svelte-tutorial/tree/lesson-7
🐱💻 🐱💻 Other Related Free Courses:
+ HTML & CSS Crash Course - https://www.youtube.com/playlist?list=PL4cUxeGkcC9ivBf_eKCPIAYXWzLlPAm6G
+ Modern JavaScript - https://www.youtube.com/playlist?list=PL4cUxeGkcC9haFPT7J25Q9GRB_ZkFrQAc
🐱💻 🐱💻 Svelte Docs
https://svelte.dev/tutorial/basics
🐱💻 🐱💻 The Net Ninja Community Boards:
https://community.thenetninja.co.uk/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Net Ninja · Net Ninja · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
More on: JavaScript Fundamentals
View skill →Related Reads
📰
📰
📰
📰
Why Vanilla JS?
In the article below, I am sharing my story of building SaaS product in vanilla js and explaining why I decided to go with this approach.
https://guseyn.com/html/posts/why-vanilla-js.html
Dev.to · Guseyn Ismayylov
How to Create a Cursor Tail Using HTML, CSS, and JavaScript
Medium · JavaScript
I built a landing page with Three.js, vanilla JS, and zero frameworks — here's what I learned
Dev.to · Ayush Shekhar
Part 1: Why I Rarely Use useEffect Anymore (and what I use instead)
Dev.to · Alejandro
🎓
Tutor Explanation
DeepCamp AI