Multiple ways of Fetching Events in React #knowledgekeen

KnowledgeKeen
26 Feb 202220:16

Summary

TLDRIn this informative video, Wasim from Knowledge Scheme returns after a hiatus to discuss event handling in React. He explains the concept of synthetic events and demonstrates three methods to bind events to HTML elements. Wasim also covers how to prevent default behaviors like redirection in links and emphasizes the importance of understanding JavaScript's event handling framework for deeper insights into React's synthetic events. The video promises to make learning React more engaging by connecting it with core JavaScript principles.

Takeaways

  • ๐Ÿ˜€ The video is from the 'knowledge scheme' channel and is presented by Wasim, who apologizes for a delay in posting due to recent events.
  • ๐Ÿ”„ The channel is back on track and will have no more delays, moving forward with new topics in React.
  • ๐Ÿ“š The video focuses on 'fetching events' in React, building on the previous discussion about synthetic events.
  • ๐Ÿ”‘ An 'event' is defined as something that happens when a user interacts with a browser, like clicking a button.
  • ๐Ÿ“ Event handling in React is similar to traditional DOM event handling, with the use of event listeners and handlers.
  • ๐Ÿ” The video explains how to track which button is clicked among multiple buttons using event properties like 'event.target'.
  • ๐Ÿ› ๏ธ Three methods of binding events to elements are discussed: inline function, passing an event object, and using the '.bind()' method.
  • ๐Ÿ”„ The first method involves writing a straightforward function for the event without additional parameters.
  • ๐Ÿ“Œ The second method allows passing additional parameters to the event handler function along with the event object.
  • ๐Ÿ‘จโ€๐Ÿ’ป The third method, less commonly used, involves binding the event to the handler function using '.bind()'.
  • ๐Ÿšซ The importance of using 'event.preventDefault()' is highlighted to avoid default behaviors like redirection in anchor tags.
  • ๐Ÿ”‘ Additional event methods like 'event.stopPropagation()' are mentioned for controlling event bubbling and capturing.

Q & A

  • What is the main topic of the video?

    -The main topic of the video is about fetching events in React and how to handle them.

  • Why was there a delay in posting the video?

    -The presenter, Wasim, mentions that there were a few things going on recently that prevented him from making and editing videos, which caused the delay.

  • What is a synthetic event in React?

    -A synthetic event in React is a cross-browser wrapper around the browserโ€™s native event. It is used to handle events in React applications.

  • How can you determine which button was clicked in a React application?

    -You can determine which button was clicked by passing the event object to your method and using properties like `event.target` to identify the clicked element.

  • What are the three ways to bind events to elements in React?

    -The three ways to bind events in React are: 1) Using an inline function, 2) Passing the event as a parameter to a function, and 3) Using the `.bind()` method to force an event binding.

  • Why is it necessary to fetch the event object in event handling?

    -Fetching the event object is necessary to access details about the event, such as the target element, and to perform actions like preventing the default behavior of an element.

  • How can you prevent the default behavior of an anchor tag in React?

    -You can prevent the default behavior of an anchor tag by calling `event.preventDefault()` within the event handler function.

  • What is the purpose of the 'stop propagation' method in event handling?

    -The 'stop propagation' method is used to prevent an event from bubbling up the event chain, which can be useful for stopping events from reaching parent elements.

  • What does the presenter mean by 'React adds its own layer of synthetic events'?

    -This means that React provides a unified interface for handling events across different browsers, abstracting away the differences in how browsers implement native events.

  • How can you pass additional parameters to an event handler function in React?

    -You can pass additional parameters to an event handler function by including them in the function call when binding the event to an element.

  • What does the presenter suggest for viewers who want to dive deeper into event handling?

    -The presenter suggests that viewers who want to dive deeper into event handling should learn about the event handling and event listening concepts of JavaScript, as React builds upon these core JavaScript frameworks.

Outlines

00:00

๐ŸŽฅ React Event Handling Introduction

The script begins with the host, Wasim, welcoming viewers back to the 'Knowledge Scheme' channel after a long hiatus due to personal reasons. He assures that the channel is now back on track with no further delays. The main topic introduced is React event handling, specifically how to fetch events. Wasim explains the concept of events in React, such as clicks and mouse events, and how they are triggered in the browser. He emphasizes the importance of understanding these events for effective React development and hints at exploring the capture of specific event details in subsequent content.

05:00

๐Ÿ” Exploring Event Fetching in React

In this paragraph, Wasim delves into the specifics of fetching events in React. He demonstrates how to bind events to elements like buttons and divs and how to identify which element triggered the event. Wasim introduces the concept of using 'event' objects to capture details about the event, such as the target element. He also explains three different methods to handle events in HTML elements, providing examples of how to implement each method and the importance of understanding which button or element is being interacted with by the user.

10:01

๐Ÿ“Œ Understanding Event Properties and Methods

Wasim continues the discussion on event handling by explaining the properties and methods available within the 'event' object. He illustrates how to use 'event.target' to determine the element that was clicked and how to differentiate between multiple buttons or elements with the same event handler. The paragraph also touches on the creation of callbacks and the potential issues with reusing event handlers without creating new ones for each element, suggesting the importance of unique event handlers for each element.

15:02

๐Ÿ›‘ Preventing Default Behavior with Events

Here, Wasim provides an example of preventing the default behavior of an anchor tag, which is to navigate to a new URL when clicked. He explains the use of 'event.preventDefault()' to stop this default action and keep the user on the current page. The explanation includes how to apply this method to an event handler to create a link that performs an action without redirecting. This part of the script highlights the control developers have over user interactions through event handling.

20:03

๐ŸŒ Conclusion and Future JavaScript Sessions

In the final paragraph, Wasim wraps up the video by emphasizing the importance of understanding events and event handling in JavaScript for deeper knowledge. He mentions that future videos will cover event handling concepts in more detail, particularly focusing on JavaScript's native capabilities. Wasim encourages viewers to like, subscribe, and provide feedback in the comments section, appreciating their support and promising more content in the future.

Mindmap

Keywords

๐Ÿ’กReact

React is a popular JavaScript library used for building user interfaces, particularly single-page applications. It is the main theme of the video script, as the tutorial discusses handling events within React components. For instance, the script mentions 'fetching events' in React, indicating the process of capturing user interactions with the UI.

๐Ÿ’กSynthetic Events

Synthetic Events are React's wrapper around the browser's native events. They provide a consistent interface for different browsers and improve performance by allowing React to optimize event handling. The script refers to synthetic events when explaining how React handles user interactions like clicks.

๐Ÿ’กEvent Handling

Event handling is a fundamental concept in JavaScript and React, where developers define functions to execute in response to user actions or browser events. The video script delves into various methods of event handling in React, such as using inline functions or passing event objects to handlers.

๐Ÿ’กEvent Listeners

Event listeners are functions that run when a specific event occurs. The script briefly touches on the concept, emphasizing that while they are important, the focus is on how React handles events, which is often more straightforward than traditional DOM event listeners.

๐Ÿ’กEvent Target

The event target refers to the specific element that triggered an event. In the script, the concept is illustrated by explaining how to determine which button was clicked by using 'event.target' to capture the element that initiated the event.

๐Ÿ’กEvent Propagation

Event propagation is the process by which an event travels through the DOM. The script mentions 'event.stopPropagation', which is a method used to stop an event from bubbling up through the DOM tree, preventing parent elements from receiving the event.

๐Ÿ’กDefault Behavior

Default behavior in web development refers to the standard action an element takes when an event occurs. The video script provides an example of preventing the default behavior of an anchor tag, which is to navigate to a new URL, by using 'event.preventDefault()'.

๐Ÿ’กCallback Function

A callback function is a function passed into another function as an argument to be executed later. The script discusses the use of callback functions in event handling, such as creating a new function each time a button is rendered, which can be problematic in certain scenarios.

๐Ÿ’กEvent Binding

Event binding is the process of attaching an event listener to a specific event on a DOM element. The script explains different methods of event binding in React, including inline event handlers and using the '.bind()' method to attach event handlers.

๐Ÿ’กES6

ES6, or ECMAScript 2015, is a version of the JavaScript language that introduced many new features, including modules, classes, and arrow functions. The script mentions using ES6 syntax when defining a function component in React, showcasing modern JavaScript practices.

๐Ÿ’กComponent Reusability

Component reusability is a key feature of React, allowing developers to create modular components that can be used in different parts of an application. The script demonstrates creating a reusable 'fetchEvent' component, emphasizing the benefits of DRY (Don't Repeat Yourself) coding practices.

Highlights

Introduction to the concept of fetching events in React after a gap in video uploads.

Explanation of synthetic events in React and their role in handling user interactions.

How to bind events to elements in React, similar to traditional DOM event handling.

Demonstration of creating a reusable 'fetch event' component in ES6.

Importing and using the 'fetch event' component in the app.js file.

Three methods of fetching events on HTML elements in React.

Using the 'onClick' handler to identify which button is clicked.

Fetching the event object to understand what is happening on the event handling chain.

Utilizing 'event.target' to find out the targeted element that triggered the action.

Passing additional parameters to event handlers for more specific interactions.

The difference between creating callbacks with event handlers and the potential for confusion.

Preventing the default behavior of elements like anchor tags using 'event.preventDefault'.

Explanation of 'event.stopPropagation' to stop event bubbling and capturing.

The importance of understanding JavaScript's event handling framework for deeper knowledge.

React's synthetic events layer that ensures consistent behavior across all browsers.

Invitation for feedback and a reminder to like, subscribe, and comment on the video.

Transcripts

play00:01

[Music]

play00:08

hey guys welcome back to our channel

play00:10

knowledge scheme and this is your friend

play00:11

wasim i know it has been very long that

play00:14

we i posted my last video

play00:17

but there were a few things which were

play00:18

going on recently and i was not able to

play00:20

make more videos as well as edit them

play00:23

and post them and hence

play00:25

this huge gap but we are back on track

play00:27

and there won't be any more delays

play00:29

henceforth so

play00:31

let's go ahead and start covering a new

play00:33

topic in react that is

play00:36

fetching

play00:37

events so

play00:39

in the last video we saw uh what are

play00:41

synthetic events right we saw some

play00:43

clicks mouse mouse on some of the other

play00:46

events that usually we have with react

play00:48

but today we are going to see how to

play00:51

fetch those events so basically what are

play00:53

these events um

play00:55

if i'm clicking on a button so that

play00:58

there is something which is happening

play00:59

the event that is triggering so that is

play01:02

an event okay so

play01:03

something which is happening on the

play01:04

browser okay after you click the button

play01:07

how the browser knows it knows by that

play01:10

event what is being clicked

play01:12

so

play01:13

that's there we go that this is the

play01:15

event that is happening so

play01:17

we are going to explore that okay how to

play01:20

fetch that particular event so that we

play01:22

can capture multiple things we are going

play01:23

to see what are the things that we are

play01:24

going to capture so basically event

play01:26

handling in react is quite similar to

play01:29

what event handling is happening in in

play01:31

our regular

play01:32

dom

play01:33

right so there are multiple events event

play01:36

listeners these are some huge words that

play01:38

we are not going to be interested right

play01:40

now okay we are going to see those in

play01:42

javascript or we have already been seen

play01:44

in some of the other videos in

play01:45

javascript so let's see um how do we

play01:49

bind these events to any of the elements

play01:51

we saw

play01:52

in our previous example that we binded

play01:54

an event on a button we minded an event

play01:56

on a div

play01:57

but if i want to fetch that particular

play01:59

event key just to give you an example

play02:01

that i clicked on a button so on my page

play02:03

there might be hundreds of buttons but

play02:06

which button i click how does the

play02:08

browser know which button is being

play02:10

clicked or let's say that being a

play02:11

programmer i want to know which button

play02:13

is being clicked so how you're going to

play02:16

track that so to track this you need to

play02:19

pass

play02:20

this this particular certain event to

play02:22

your methods the method that we wrote in

play02:23

a previous video if you have not seen

play02:25

that video i'll just mention it

play02:27

somewhere up here you can just click it

play02:29

and you can just go back to our previous

play02:30

video watch that and then you can come

play02:32

back to this video so you have a better

play02:34

understanding what we are talking about

play02:37

so

play02:38

let's dive into fetching these events

play02:40

let's not talk anymore let's see how

play02:42

exactly that is happening

play02:44

so let me just click

play02:46

create a folder i'm sorry i have just

play02:48

lost my

play02:50

laptop and hence we are going to create

play02:52

some new components fresh pages

play02:55

everything fresh

play02:56

so

play02:57

let me just add

play03:00

fetch event a new component

play03:03

dot js

play03:04

because it is a javascript so how do we

play03:06

write using es6 let's say that i write

play03:10

oops

play03:13

fetch event

play03:15

as a function

play03:17

it's a simple plain javascript es6

play03:19

function and basically we are going to

play03:21

export it

play03:22

export default fetch event

play03:28

so now this is a

play03:30

reusable

play03:32

function

play03:34

so we can use it again we just need to

play03:36

import it and use it and let me just try

play03:38

to see if

play03:40

if we are able to use this fetch event

play03:42

ctrl s save

play03:44

let's go to our app.js we used to do

play03:46

like this right and go into our div

play03:49

and add this

play03:51

fetch

play03:52

event

play03:54

component you see the input is already

play03:56

being placed we have seen this multiple

play03:58

times we have seen how to write a

play04:00

component we are continuously seeing it

play04:02

again and we are going to see it in all

play04:03

the future videos because that's the way

play04:05

we learn by doing trying and running it

play04:08

multiple times

play04:10

so fetch event

play04:12

this is the event okay let's go to our

play04:14

browser fetch event yes we have it over

play04:17

here so on this fetch event

play04:20

let's try to add a button

play04:22

so before we just try to

play04:25

start fetching the event i'll just like

play04:27

to explain you that there are three ways

play04:29

you can

play04:30

fetch the event

play04:33

on any any kind of a button dave or any

play04:35

element in html so whenever you write

play04:37

some function on click on

play04:39

on

play04:40

double click on mouse up mouse down key

play04:42

or key down there are so many events we

play04:44

have but on any of these

play04:47

functions that we have okay even

play04:48

handlers you can say so on click is an

play04:50

even handler

play04:52

so whenever you click something so you

play04:54

write some function to handle that event

play04:56

correct

play04:57

so there are three ways to do that let's

play05:00

see those one by one we have seen one in

play05:01

the previous video that was like

play05:03

let me just quickly

play05:05

add a simple button

play05:07

right

play05:08

on click equal to

play05:12

let's say that

play05:13

i just write

play05:15

um click here

play05:18

so it's a simple button and this is like

play05:20

my

play05:22

click handler

play05:25

right and this click handler is nothing

play05:27

but a simple again function like click

play05:30

handler

play05:32

this the same way we have written it up

play05:34

i just write a console.log

play05:37

let me just make it a bit bigger so that

play05:39

you can see it more clearly i hope this

play05:42

is visible now

play05:43

click handler i am being clicked

play05:50

okay

play05:51

and

play05:52

save

play05:53

just go to the browser we have a click

play05:55

button

play05:56

i hope you can see it now

play05:58

let me just go to our console

play06:00

oops

play06:02

let me just

play06:04

increase the size of my console and

play06:06

whenever i clicked

play06:07

you can see sorry i double clicked it

play06:10

whenever you click it will

play06:12

just call it this we have seen it on in

play06:14

a previous video right so this is the

play06:17

first way what i did i have a on click

play06:21

method right on click event okay and

play06:24

what i'm doing i'm just passing a simple

play06:27

function to that particular event so

play06:29

whenever this button is clicked on click

play06:34

this click handler function or whatever

play06:37

function this is our custom name so you

play06:39

can just give it a call and it will be

play06:42

called

play06:43

simple very easy

play06:46

but now i want to see let's say that

play06:48

the previous example that i was saying

play06:51

let's see that let's say i have

play06:55

i'll just copy paste this button a few

play06:57

times

play07:04

oops

play07:06

so i have written this multiple times

play07:08

let's say that i have button one

play07:11

button two and button three

play07:13

so

play07:14

i have three buttons if i click here

play07:17

it's calling the same thing if i'm

play07:19

clicking here it's calling the same

play07:20

thing if i'm clicking here it's again

play07:22

calling the same thing so how does my

play07:24

browser know which button is called when

play07:28

correct i can add multiple events okay

play07:31

we are not going to just write some do a

play07:34

copy pasting for the even handlers but

play07:35

that's okay

play07:36

so how does my browser know or let's say

play07:38

that i just want to know

play07:41

which button is being called so how you

play07:43

can track that to make it more easier

play07:46

how is how we used to do it in

play07:48

javascript was simply fetch the event

play07:51

and to fetch the event you can there

play07:54

there is a default parameter that is

play07:56

always passed into the uh method that is

play07:59

even you can just write e you can write

play08:02

complete name event you can write evt

play08:04

just

play08:04

a sensible name so that you know what is

play08:08

being called so if i just write this evt

play08:12

and if you just pass it to the console i

play08:14

just pass it to the console

play08:17

let's see what is happening

play08:18

if i click here you can see that there

play08:21

is a synthetic base event and which is

play08:24

the event that is an on click which is

play08:26

triggered so now you are able to capture

play08:29

that event yes there was something which

play08:31

was happening if i just make it like

play08:34

instead of on click

play08:36

let's say that i do it on

play08:38

double click

play08:40

oops

play08:41

my bad

play08:42

so now the first one is simple on click

play08:45

the second one is on double click the

play08:47

third one is on click so let's see what

play08:49

happens

play08:50

if i click here

play08:51

that's a synthetic based event on click

play08:54

here i'll do a double click

play08:56

oops

play08:57

on double click so you're capturing the

play08:59

events now so you know what is happening

play09:02

so the browser itself knows what is

play09:04

happening on the

play09:05

uh

play09:06

event handling chain but as a developer

play09:09

you also know

play09:11

which event is being triggered

play09:16

if i want so let's dive a bit more if i

play09:19

want to know

play09:21

let me just refresh it

play09:23

which button is called or you just want

play09:24

to know which element

play09:26

is being triggered or which is the

play09:29

targeted element on which this whole

play09:30

thing is happening so let's say that if

play09:32

i'm clicking on button one click here

play09:34

one so my targeted element is the button

play09:37

first the first button that i have

play09:39

written

play09:40

so to know that you just have to pass

play09:43

there are multiple properties

play09:45

which comes with an event okay so the

play09:49

simplest one is event dot target so you

play09:52

can find out what was the targeted

play09:55

element which was been performed the

play09:57

action was performed on

play09:59

so if i just write event dot target or

play10:01

ev target the parameter basically that

play10:04

you are passing

play10:05

so that dot target

play10:07

refresh

play10:10

see

play10:11

button click here one so this was the

play10:13

element which was the targeted element

play10:16

if i click on the second floor second

play10:18

one because it was a double click

play10:20

here

play10:21

and

play10:22

see you can simply find out

play10:25

which targeted element you are

play10:29

calling now let's jump on to the second

play10:30

way of doing this

play10:32

the first

play10:34

way is done so the first way was i am

play10:36

just writing a simple plain

play10:38

straightforward function

play10:40

i am passing

play10:42

nothing to it

play10:43

let's say that whenever i'm clicking

play10:45

something okay so i'm just clicking the

play10:49

button let me just change this also to

play10:52

on click it's just irritating me

play10:54

okay

play10:55

i want to pass some data so i just want

play10:58

to recognize instead of doing this event

play11:00

or target and everything i just want to

play11:02

pass a simple parameter to this function

play11:04

okay that this is the button one this is

play11:06

button two and three or something like

play11:08

that so how we can do that so instead of

play11:10

an event

play11:11

i also want to pass a parameter with

play11:15

this event

play11:16

to do this you just need to make a small

play11:18

change

play11:19

that is

play11:21

calling the function like this

play11:25

this is the second way of binding an

play11:28

event to this function the handler

play11:32

if i just save this right now

play11:37

i just need to pass an event because i

play11:39

am catching the event there this is

play11:41

again the same thing you just don't only

play11:43

need to write e

play11:45

you can just write anything over here

play11:47

and the thing that i'm passing to this

play11:49

function is an event so

play11:51

i'm writing an event and i'm passing the

play11:53

same thing if i write an e you need to

play11:54

pass the e you if you're writing evt you

play11:57

need to pass it you're if you're writing

play11:58

knowledge king you need to pass the

play11:59

knowledge gain but the first parameter

play12:02

whatever you're passing

play12:03

is nothing but a event you need to

play12:06

recognize c there is no change if i just

play12:09

do it there is nothing which is changed

play12:12

both of them are

play12:14

one and the same so there is a slight

play12:16

problem with this the first the second

play12:18

approach that you are doing

play12:20

what does this do

play12:22

this is this basically creates a

play12:24

callback

play12:25

with each whenever this hole the button

play12:28

one is rendered so

play12:30

this is a reusable let's let's take it

play12:32

like this that this is a reusable event

play12:35

okay so yeah um to make it just dive it

play12:38

in a bit deeper

play12:40

so what is happening over here

play12:42

whenever the if i'm rendering this page

play12:44

100 times it is just doing the same

play12:46

thing it is reusing the same thing it is

play12:49

not creating something new but whenever

play12:51

i am

play12:52

rendering this multiple times the same

play12:53

component if i'm rendering it multiple

play12:55

times a different

play12:56

a different callback is created simple

play12:58

but let's not dive in deep let's not get

play13:01

more confused here we'll see that a bit

play13:04

later in a future videos when we are

play13:06

going to see it again

play13:07

but

play13:08

to make it more simpler we can just do

play13:10

this one and the same so both are one

play13:13

and the same for now i'll just copy

play13:15

paste it and

play13:18

click here too i want to pass another

play13:20

parameter that is let's say that btn1

play13:23

and the second parameter is

play13:25

this is my second button

play13:27

so this is my additional parameter i am

play13:29

passing it i need to catch it in my

play13:32

function extra parent

play13:36

i just write it any name so basically

play13:38

whatever you're passing you can catch it

play13:40

with any name so extra parameter what i

play13:42

just say i'll just write another

play13:44

console.log

play13:47

another parameter

play13:50

comma

play13:51

i'll just

play13:52

print this extra parameter

play13:55

refresh

play13:57

whenever i'm clicking the first button

play13:59

another param that is button one so you

play14:01

can catch the additional parameters as

play14:04

well

play14:05

by using the

play14:06

second type so this was the second way

play14:09

by which you are calling the

play14:11

method

play14:12

okay

play14:13

now let's jump on to the third way to do

play14:16

that

play14:17

but

play14:19

we

play14:20

this isn't just to know knowledge the

play14:22

third type we usually don't do this okay

play14:25

if you want to write the event handling

play14:26

so if you want to write an on click and

play14:29

add functions to it you just do either

play14:31

way by the first way or the second way

play14:33

the third way is a just to know

play14:35

knowledge because that is a core

play14:36

javascript way that we usually do that

play14:39

is bind the event forcefully so how to

play14:41

do that

play14:43

so let's do this okay what i did over

play14:45

here was i just added a bind so

play14:48

basically i'm binding this to this event

play14:50

handler but the simple problem over here

play14:53

is you can find the target and

play14:55

everything i am being clicked and click

play14:58

handler.bind so i have binded this

play15:01

okay and whatever parameter you want to

play15:03

pass you can just pass it something like

play15:05

this for the further use

play15:08

this is the way

play15:11

i'll just make it

play15:12

like this

play15:14

all the three things in one bow

play15:17

just to for you to understand

play15:21

so these are the three ways that you're

play15:22

saying click here one was the first way

play15:24

cliche 2 was the second way of doing

play15:27

that and click here 3 was the third way

play15:30

of handling these

play15:31

events this was easy right react makes

play15:34

it more easier and it's quite wonderful

play15:37

to do this thing but

play15:39

why do we do this why do we need these

play15:41

events

play15:42

i'll just give you another simple

play15:44

example that why really we need these

play15:47

events

play15:48

let's take an example that instead of

play15:50

buttons

play15:51

as i said i can write these event

play15:54

handlers on any html element

play15:57

what if i want to write something like

play15:59

this

play16:00

a href is equal to www dot our favorite

play16:02

site google.com

play16:04

and let's say that click here is another

play16:10

it's a simple link

play16:11

if i click it it just opens the

play16:14

google.com oops i did something wrong it

play16:17

should be http colon double forward

play16:19

slash so that it opens it wonderfully

play16:23

it's just appending here

play16:27

if i click it it just takes me back to

play16:29

the

play16:31

it just takes me to the

play16:32

google site right

play16:34

but

play16:35

i don't want my website to be redirected

play16:38

somewhere else let's say that instead of

play16:40

google i have something just

play16:44

something test okay

play16:46

if i do this if i just click it it just

play16:48

appends it to my address bar which i

play16:50

really don't want i don't want it to

play16:52

redirect i just want a simple

play16:54

link

play16:55

which should be clicked

play16:57

what if you want to avoid this default

play17:00

behavior of anchor that it forcefully

play17:02

takes you to a different address here my

play17:05

address is changing right

play17:06

it is forcefully taking me to a

play17:08

different address which i really don't

play17:09

want so how you can avoid this okay how

play17:12

you can prevent a default behavior to do

play17:15

that we really need a event so what will

play17:18

happen if i just say that on click

play17:24

i have this

play17:26

oops

play17:27

click handler this is again doing the

play17:28

same thing if i'm clicking here it's

play17:30

just fetching me the event and the

play17:32

element right now but it is not

play17:35

preventing my behavior so to prevent the

play17:37

behavior

play17:39

as i said event has multiple properties

play17:41

and methods too so there is a method

play17:45

that is

play17:46

evt i am using the same variable dot

play17:49

default oops

play17:51

prevent

play17:53

default

play17:54

see i'm writing it slowly so that you

play17:56

understand this

play17:58

this is core javascript

play18:00

okay

play18:01

whenever you want to prevent a default

play18:03

behavior of a javascript or of a button

play18:06

a link a form submission anything you

play18:09

want to prevent something default which

play18:11

is a default behavior of an element like

play18:13

anchor stack's default behavior is to

play18:15

take it to a new site form submissions

play18:17

default behavior is to take to submit

play18:19

the home whole form to a different thing

play18:21

so if you want to avoid this default

play18:23

behaviors you just need to write event

play18:26

dot prevent default and voila i just do

play18:28

this

play18:29

even now if i click it hundred times

play18:33

see

play18:34

it is clicking but it is not taking me

play18:36

to a new address

play18:38

easy so there are multiple things so

play18:40

event dot prevent default is there event

play18:42

dot stop propagation is that uh is there

play18:45

so that it stops the default uh bubbling

play18:48

capturing of events and there are a lot

play18:50

of things that you can do with events so

play18:52

this is a short video i hope this was

play18:54

interesting to make you understand how

play18:56

you can fetch these events how you can

play18:58

use this event and you can do a multiple

play19:01

things with these events and this there

play19:02

are wonderful things that you can do

play19:03

with even handling

play19:06

if you want to just learn it

play19:08

deep okay if you want to dive deep into

play19:11

events you have to learn the event

play19:13

handlings and even listening concepts of

play19:15

javascript which is really interesting

play19:18

we are going to make videos on that in

play19:19

our javascript sessions but not into

play19:21

react because react also does the same

play19:23

thing hand in hand it just adds an

play19:25

overlay onto the core javascript event

play19:28

handling framework so that it works

play19:30

similar on all the browsers so react

play19:33

adds its own layer of synthetic events

play19:35

on the core javascript image that that's

play19:37

the reason i said in my previous video

play19:39

that we don't write on clicks like this

play19:42

there is no on click small

play19:44

it's written in camel casing

play19:48

because react adds an additional layer

play19:50

on this

play19:51

i hope this was interesting i hope this

play19:53

video makes sense and

play19:55

we are back on track and

play19:57

you all know that you have just have to

play19:59

click the like and subscribe button to

play20:01

keep on watching our videos and please

play20:02

please please do come let me know in the

play20:04

comment section what you really like and

play20:06

what you you really don't like from my

play20:08

videos i know you guys have been doing

play20:10

this and i really appreciate it thank

play20:13

you so much for giving this all this

play20:14

love

play20:15

see you soon

Rate This
โ˜…
โ˜…
โ˜…
โ˜…
โ˜…

5.0 / 5 (0 votes)

Related Tags
ReactEvent HandlingSynthetic EventsJavaScriptDOM ManipulationButton ClicksEvent ListenersPrevent DefaultTutorialWeb Development