Svelte Tutorial for Beginners #7 - Inline Event Handlers

Net Ninja · Beginner ·🌐 Frontend Engineering ·6y ago

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 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

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
Learn why choosing Vanilla JS can be a great approach for building SaaS products and how it can simplify development
Dev.to · Guseyn Ismayylov
📰
How to Create a Cursor Tail Using HTML, CSS, and JavaScript
Learn to create a colorful cursor tail using HTML, CSS, and JavaScript for a unique user interface effect
Medium · JavaScript
📰
I built a landing page with Three.js, vanilla JS, and zero frameworks — here's what I learned
Learn how to build a landing page with Three.js and vanilla JS without relying on frameworks, and discover the key takeaways from this project
Dev.to · Ayush Shekhar
📰
Part 1: Why I Rarely Use useEffect Anymore (and what I use instead)
Learn why useEffect is not always the best choice in React and discover alternative solutions
Dev.to · Alejandro
Up next
How To Build A Twitter Clone - React Next JS - Appwrite Crash Course
Adrian Twarog
Watch →