Full React Tutorial #7 - Click Events

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

Key Takeaways

Handles click events in React components using the onClick event handler

Full Transcript

all right then so when we have a website typically there's loads of different events going on inside the website when a user browses it things like hover events or click events or form submission events keyboard events scroll events loads and loads now in this video we're just going to look at click events which is when a user clicks on something and i'm going to show you how i can react to those events by running some code so first of all let's create a button inside the home component underneath the h2 and the text is going to be click me so i want to react now to a user clicking on this button and i want to fire a function when they do that so first of all i'm going to create this function inside the home component function at the top and we'll store it in a constant which i'm going to call handle click and typically you'll see the different functions called something like this like handle click or handle mouse over or handle submit or something like that that's just a naming convention but you can call the functions whatever you want so inside this arrow function all i'm gonna do is log a message to the console and the message is gonna be hello ninjas all right so if i now save this nothing's going to happen when i click on this because i've not linked up this function to this button so it doesn't know to run that function yet let's open the console already anyway just so we can see that in a minute so then how do we hook this up well all we need to do is come to the button and say on click with a capital c and set that equal to a dynamic value so curly braces and we just pass in a function reference so whatever the function is called which is handle click now we don't invoke the function because if we were to save this then it's going to invoke this automatically without the user even clicking on it and we can see this log to the console right away we don't want to do that we don't want to invoke it we just want to set a reference to that function right here then when a user clicks on this it will invoke that function for us so let's save this i'm going to clear out the console and let's click on this and it works click again it does it again so that's how we can create a function and assign it to an element so that it fires when a user clicks on it by using this on click property right here and then passing in a function a reference so what if we want to pass in an argument to the function for example if i wanted to pass in a name well normally what we do is use parentheses and pass in the name for example yoshi right but like i said before if we do this then it's going to automatically invoke this function right away because the parentheses invoke the function so we can't do this if we want to pass in an argument instead we have to wrap this inside an anonymous function so i'm going to do another button to demo this so i'll say click me again and then up here i'm going to create another function so const and i'll call this handle click again and set it equal to an arrow function and inside here we're going to take as a parameter the name okay and i'll log that to the console so console.log and then i'll say hello and i'll concatenate the name so plus name you could use a template string if you wish i'm just using concatenation so now i want to find this function when we click on this button so again on click is equal to a dynamic value which is handle click again okay so now if we save it it's going to fire this function but we're not passing in the name here and like i said we can't pass that in directly right here because it's going to invoke that function so how do we do this well we do need to pass in the argument so let's just do that i'm going to pass in mario and then what we need to do is then wrap this inside an anonymous function so let me cut this for now and instead i'm just going to do an arrow function right here like so now this is a function which will then fire when a user clicks on this button so inside here i could just say console.log hello right and this is going to do the same thing as this we're not invoking this function right here we're just referencing it right it's an anonymous one we don't store it in a constant or anything like that we just create it directly here but we're still not invoking that and then when a user clicks on this button it's gonna fire the function so let's test this out first of all click me again and we see hello so now what we could do is inside this function we could just invoke this other function so now this is not going to be invoked straight away because it's being wrapped in an anonymous function which isn't firing to begin with so only now when we click on this button it fires this function and then invokes the other one which passes in the name so if i save this now it should work click me again hello mario awesome so that works now if i wanted to i could do this all in one line by bringing this up and bringing this up as well and we don't really need these curly braces now because it's on one line for the function so let's delete that one and this one we still keep these curly braces because this signifies a dynamic value the other ones were just for the anonymous function so this is still going to work let me save that and test again and we see hello mario awesome so that works now one more thing i want to show you is the event object or the event parameter that we automatically get access to inside these functions when an event occurs so this one right here where we just reference a function it automatically gains as the first parameter the event object and then we can do something with that event object let me just log it to the console so you can see the different information on that object if i save it and come over here and click this first button we see hello ninjas but then also this event object and we can see all of these different properties about the event if we wanted to use them now what about the other one because we don't automatically get it as a first argument inside this function because it's not being referenced as the function this one right here this anonymous function gets access to the event object automatically then if we want to use it inside our custom function we can just pass it in as an argument and it can be first or second it doesn't really matter so i can pass it in right here so i take the automatic argument from the function that wraps this and pass it into this custom function as a second argument and then i can use it inside this function so after name i could say e and then say i just want the target i could use the target property for example that was just one of the properties on this object and then if i click on this we can see we get the event target awesome so that's how we can react to click events in our components

Original Description

In this React tutorial we'll talk about events & how to react to them in our components - specifically, we'll look at click events. 🐱‍💻 🐱‍💻 Course Files: + https://github.com/iamshaunjp/Complete-React-Tutorial 🐱‍👤🐱‍👤 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 - https://www.thenetninja.co.uk/udemy/vue-and-firebase + D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase 🐱‍💻 🐱‍💻 Helpful Links: + HTML & CSS Course - https://www.youtube.com/watch?v=hu-q2zYwEYs&list=PL4cUxeGkcC9ivBf_eKCPIAYXWzLlPAm6G + Modern JavaScript course - https://www.youtube.com/watch?v=iWOYAxlnaww&list=PL4cUxeGkcC9haFPT7J25Q9GRB_ZkFrQAc + Get VS Code - https://code.visualstudio.com/ 🐱‍💻 🐱‍💻 Social Links: Facebook - https://www.facebook.com/thenetninjauk Twitter - https://twitter.com/thenetninjauk Instagram - https://www.instagram.com/thenetninja/
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

📰
I Found a Surprisingly Fun Way to Practice Frontend Development
Practice frontend development in a fun way by building football-themed applications on VibeCode Arena
Dev.to AI
📰
The Enter key that submits your form while a Japanese user is still typing
Learn how to prevent the Enter key from submitting a form while a Japanese user is still typing, and why it matters for user experience
Dev.to · greymoth
📰
The two-Reacts bug: when packages aren't singletons
Learn to fix the two-Reacts bug by understanding how to handle non-singleton packages in React applications
Dev.to · r9v
📰
🚀 Introducing Prism Guard — An Open Source Frontend Architecture Intelligence Platform
Learn about Prism Guard, an open-source frontend architecture intelligence platform, and how it can help improve codebase quality
Dev.to · Ritumoni Sarma
Up next
The masks we wear | Zora Krstić | TEDxLuxembourgCity
TEDx Talks
Watch →