ReactJS Tutorial - 14 - Binding Event Handlers

Sdílet
Vložit
  • čas přidán 6. 09. 2024
  • 📘 Courses - learn.codevolu...
    💖 Support UPI - support.codevo...
    💖 Support PayPal - www.paypal.me/...
    💾 Github - github.com/gop...
    📱 Follow Codevolution
    + Twitter - / codevolutionweb
    + Facebook - / codevolutionweb
    📫 Business - codevolution.business@gmail.com
    In this video, lets learn how to bind event handlers in React components.

Komentáře • 280

  • @isaacdruin
    @isaacdruin Před 4 lety +112

    "so in this video, I will go over all of them"
    What a legend.

  • @vinitraut1425
    @vinitraut1425 Před 5 lety +295

    4:25 'this' keyword is undefined because this used with a function returns 'window' object on the browser and 'global' object inside nodejs environment. Since 'react strict mode' is enabled, it is returning 'undefined'.

    • @dasunanushka8774
      @dasunanushka8774 Před 4 lety +5

      Yes. This is the actual reason it gives undefined.

    • @meghalbisht6768
      @meghalbisht6768 Před 4 lety +1

      please elaborate a little

    • @akashgupta1353
      @akashgupta1353 Před 4 lety +1

      hey please elaborate more

    • @jatintayal4358
      @jatintayal4358 Před 4 lety +13

      arrow functions don't create their own context so there it worked without binding.

    • @pradeepsaravanan4348
      @pradeepsaravanan4348 Před 4 lety +151

      @@akashgupta1353 First of all "this" keyword points the Parent element on which it is called . So when you use a Named function with the "this" keyword inside it , it isn't binded to anything at all ..So the control passes to the "Window" element . So the Window element acts as a parent to the Named function . We see "Undefined" here because we use "Strict mode " in Javascript . What Strict Mode does is, if there is a named function( without any Bind method ) it transfers control to the parent element(window element) as i said ..this action is blocked by the strict mode ..So you see "undefined" in there ...However if you take off the strict mode ,you should see it will return the Window object ... The One Stop Solution to all of this is using arrow functions which takes parent element as the Class and not the window object .

  • @satisfyingindustrialequipment

    I rarely comment on CZcams but your tutorial and the way you explain is mind blowing i wish if I can support you financially now, but one day I will when I get a job. Thanks a lot for great content

    • @architectsmusicgroup
      @architectsmusicgroup Před 2 lety +2

      True! Clear and concise! Very helpful.

    • @zeeu
      @zeeu Před 2 lety +3

      I hope you already landed your dream job

  • @dylanoh3425
    @dylanoh3425 Před 4 lety +86

    This channel is definitely underrated. It deserves more attentions!! Great work man.

  • @toannew
    @toannew Před 3 lety +19

    4:30 why event binding is important
    5:00 use bind()
    6:20 2nd method: use the arrow function
    7:57 3rd method (official and mostly used): binding from constructor
    9:40 4th method: define the trigged function as an arrow function

  • @TheInfamousAlpha
    @TheInfamousAlpha Před 4 lety +91

    (this) was great explanation

  • @Daniel-nb3kk
    @Daniel-nb3kk Před 4 lety +14

    You explain concepts and ideas so well a mere child will understand you. Great job!

  • @vishnugajulapalli5305
    @vishnugajulapalli5305 Před 3 lety +8

    1. Binding in render
    2. Arrow function in render
    3. Binding in the class constructor
    4. Class property as arrow function

  • @GeryTeague
    @GeryTeague Před 8 měsíci +1

    Great presentation. I would recommend a variation on binding in the constructor. Instead bind on method creation in the class.
    clickHandler = function(){ ... }.bind(this);
    This keeps the binding with the function when it is created.

  • @nishantagrawal6244
    @nishantagrawal6244 Před 3 lety +6

    After a hell lot of reading stackoverflow and blogs, I got everything under the same roof and now every single approach seems relatable. Thanks a ton man! God bless you.

  • @taharasheed2096
    @taharasheed2096 Před 3 lety +8

    One of the best tutor ( I watched too many tutorials for react but this one is really the best...Even he taught how to deal with logical errors )

  • @mjdtoto615
    @mjdtoto615 Před 4 lety +8

    In regular functions the this keyword represented the object that called the function, which could be the window, the document, a button or whatever.
    With arrow functions the this keyword always represents the object that defined the arrow function. This is from W3School.

  • @edwinvarghese
    @edwinvarghese Před 3 lety +2

    showed all the mistakes we could possibly make and proceeded to fix all of them one by one. Thanks a ton for this tutorial. You are a legend.

  • @ANOTHERWOOK
    @ANOTHERWOOK Před 3 lety +2

    I honestly felt like a dumbass until I stumbled upon your channel. You explain everything in such a chill manner in which I can understand easily... highly underrated!

  • @MrBelleblaas
    @MrBelleblaas Před 3 lety +5

    Absolutely exellent.
    My threshold to continue with React has been set
    from now.
    Thank you very much.

  • @PragnaPresence
    @PragnaPresence Před 2 lety +2

    Most underrated channel with mind-blowing content with clean and crisp explanation. I rarely comment for a tech YT but this channel is beyond amazing. Many wow factors in the explanation and explanation of the reasons behind the regular usage.

  • @souvikkar5515
    @souvikkar5515 Před 3 lety +3

    Bruh! the best react tutorial on the whole YT. Small yet detailed videos.

  • @VOLTDOGmusic
    @VOLTDOGmusic Před 5 lety +11

    One of the best teachers on CZcams, thanks bruddah!

  • @KelleyKouture
    @KelleyKouture Před 4 lety +10

    FinallFinally!!!! A video that actually helped me and explained it so well! Thank you so much!!

  • @simbarashemaunga5575
    @simbarashemaunga5575 Před rokem +1

    It's really great how you give examples for both class based and functional components.

  • @maghani.official
    @maghani.official Před 4 lety +6

    I was exactly looking for something like this and honestly I salute you for the way you explained and structured the code for us to learn. I feel like an expert Reactjs Developer after watching this lol

  • @xavier.antony
    @xavier.antony Před rokem +1

    You guys are professional teachers. Awesome..

  • @johnm7571
    @johnm7571 Před rokem

    in 2023 ur videos are the best from all other tutors

  • @kumarp5632
    @kumarp5632 Před 4 lety +2

    You made ReactsJs very simple and clean to understand.. Great Work and Kudos

  • @batulmajid
    @batulmajid Před 4 lety +2

    After long research, i finally find something which satisfies me. Thanks Man!

  • @devGamersCoders
    @devGamersCoders Před 2 lety +2

    "this" is undefined because this works perfectly if the function is in a method or have object as a wrapper but here we are wrapping inside a class not object that's why this is undefined so binding it is a solution inside that constructor else use arrow function

  • @Paparoo24
    @Paparoo24 Před 3 lety +3

    Arrow functions are very useful in this case. I prefer the last approach.

  • @tannerbarcelos6880
    @tannerbarcelos6880 Před 4 lety +1

    Best explanation of bind() ! As a Java and C++ guy in school, it was easy to understand this. OOP for the win

  • @pipertripp
    @pipertripp Před 5 lety +4

    Also, Vishwas, this is an excellent series. Well done mate.

  • @RegularExpression1
    @RegularExpression1 Před 5 lety +3

    Excellent brief tutorial that covers topic well in a few minutes time. Thanks.

  • @ChillCityNaveen
    @ChillCityNaveen Před 4 lety

    This tutorials are very much interested not even single minute am feeling bored and very much detail on each concept thanks my dear.

  • @AnotherLeetcode
    @AnotherLeetcode Před 4 lety +2

    This is the first video I've seen that effectively explains 'bind(this)' well. Thank you!!

  • @yaw199
    @yaw199 Před 3 lety +1

    I think I need to comment on your videos. You are genius and thanks for such an incredible explanations. You deserve more than 5⭐⭐⭐⭐⭐⭐. I was confused with react ideology till your videos saved me lol. Bravooo!!!

  • @ShreyaskarART
    @ShreyaskarART Před rokem

    This course so far is the most intuitive one, I'm loving it !

  • @alexsampaio9198
    @alexsampaio9198 Před 3 lety +1

    You're a legend, man. Keep it up.

  • @aqibsaeed3229
    @aqibsaeed3229 Před 2 lety

    Just Excellent and Simplest way of explaining all cases with sequence and basic example. Keep up great work.

  • @sanjeetsahni4998
    @sanjeetsahni4998 Před 5 lety

    For beginners this video are must watch.. I got lot of confusion but after watching these videos everything is clear now..

  • @nakimapi
    @nakimapi Před 3 lety +1

    such a clean way of teaching!

  • @zackstam
    @zackstam Před 2 lety +1

    great tutorial, very helpfull, thank you

  • @avinashsuntosh8620
    @avinashsuntosh8620 Před 4 lety +1

    Vishwas, you rock man! Thanks so much for these beautiful tutorials. Much appreciated.

  • @mohdsalim3790
    @mohdsalim3790 Před 3 lety

    sir i like your way of teaching...you explain every concept in a easy way

  • @priyankas7136
    @priyankas7136 Před 4 lety +1

    Yes. This tutorial is amazing. As a beginner I am getting to learn alot. Very good work Mr Vishwas

  • @zahoorhussain196
    @zahoorhussain196 Před 3 lety

    I am going to watch all the videos just because the way you explained

  • @i_am_hugeng4120
    @i_am_hugeng4120 Před 2 lety

    You deserve more seubscriber and more viewer Sir, i started this reactJS Tutorial series to prepare my self in learning your nextJS Tutorial series. Thank you so much.

  • @azar1608
    @azar1608 Před 4 lety +3

    What a great explaination man, I understood the basic of react from your course, thanks for sharing

  • @danishali4665
    @danishali4665 Před 4 lety

    One of the best tutorials for Javascript and React library, I must say it`s a great contribution for developer community thankyou so much appreciate it.

  • @abdokhalaf6188
    @abdokhalaf6188 Před 3 lety +1

    Generally speaking in JavaScript, you can change the context of the "this" keyword of any function,not including the arrow function for sure, using the apply, call or bind methods by passing the object you want the "this" keyword to refer to as the first parameter of these methods
    But here we can only use the bind method since both apply and call methods will call the function directly without the event a.k.a the click event, making your application enters a sort of infinity function calls

  • @thekidwithadream
    @thekidwithadream Před 2 lety +3

    I don't get it like haven't we already used onClick event listener in the previous video where we made a counter

  • @apurvsawant5703
    @apurvsawant5703 Před 4 lety +1

    Thank you for providing this content. Great Work.

  • @prateek4524
    @prateek4524 Před 4 lety +1

    you're a savior. Thankyou so much 😊😊🔥🔥

  • @zoomworld767
    @zoomworld767 Před 4 lety

    thank you so much for your classes, you are the best teacher
    please upload javascript classes

  • @sm-fj4yt
    @sm-fj4yt Před 4 měsíci

    As fer I understand in 3:54 , under the button the value of this is buttonObject. So literally {this.clickHandler} is same as {buttonObject.clickHandler}. Bt the outer clickHandler where clickHandler method is definedn is in scope of EventBind object. So this value is EventBind outside. So we can say this.state is same as EventBind.state. which is not accessible to the event handler in button. Bt for arrow function the this value is same as enclosed lexical context means for arrow function this value becoms EventBind object. So this.state is accessible if we use arrow function. thats wthe reason for arrow function we dont get undefined also...
    Pls correct me if wrong. It will be helpful for me to clear the concepts😊

  • @kavyashribj
    @kavyashribj Před 5 lety +4

    Thanks for all the effort Vishwas! I have been following all your tutorials. They are simple and straightforward. Would like to know if you could help creating a video about basic idea behind Redux/Flux. All the other tutorials out there are really confusing :(

  • @namanjindal3089
    @namanjindal3089 Před 2 lety +2

    The main thing in here was to related the react architecture of components with this, which you have skipped over. That's the main explanation every other thing is pretty common with every tutorial

  • @shvideo1
    @shvideo1 Před 5 lety

    Excellent comprehensive and clear instruction. Very helpful. Thank you.

  • @Wolverine_261
    @Wolverine_261 Před měsícem +1

    Why does writing arrow function within class and passing it as event handler, makes it run infinite times??

  • @Shahidulidseacademy
    @Shahidulidseacademy Před 3 lety

    This tutorial is very useful for me, Thank you Codevolution !

  • @maiaklimenko6614
    @maiaklimenko6614 Před 2 lety

    Thank you for the amazing and simple explanation!

  • @akash4517
    @akash4517 Před 4 lety

    isn't this video is same as setState method video that you taught earlier .except you taught few other methods to bind.
    great work man. thanks

  • @ExpertGirl-i5w
    @ExpertGirl-i5w Před 14 dny

    best best best channel in 2024

  • @tungngo2445
    @tungngo2445 Před 5 lety

    Your video helps me tremendously. Thank you so much. You are a great teacher

  • @utkarshagrawal3640
    @utkarshagrawal3640 Před 3 lety

    To tackle it either we can bind or use an arrow func!

  • @nareshkumar-ug5sw
    @nareshkumar-ug5sw Před 2 lety

    Excellent explanation

  • @user-oh5vo5rj6y
    @user-oh5vo5rj6y Před 4 lety

    I think the first solution will be better if you place .bind at the constructor like this:
    constructor(props) {
    super(props);
    this.state = {
    message: 'Hello',
    };
    this.clicker = this.clicker.bind(this);
    }
    The benefit is that it woun't rerender every time the button has clicked.
    Update: my bad, you've already told it, but i wrote this comment when i watched only half of the video

  • @ramsinassyrian7904
    @ramsinassyrian7904 Před 3 lety +1

    You are legend man

  • @aakankshajha2080
    @aakankshajha2080 Před 2 lety +1

    Why do we call the eventHandler like this eventHandler() while using arrow functions?? And not while using the bind keyword?

  • @visweswarbk7611
    @visweswarbk7611 Před 2 lety +1

    Hi - Great presentation Gopinath ! I have a question - How come the "this.state " work in video 11 when we updated the count using setState ? Thanks

  • @lintstudios3072
    @lintstudios3072 Před 2 lety

    Great video! Helped me so much! keep up the great work.

  • @sorathavithey890
    @sorathavithey890 Před 4 lety +1

    I really love your video brother.

  • @codedrive7910
    @codedrive7910 Před 3 lety

    thanks a lot for
    clearing my doubt

  • @AZnuclear
    @AZnuclear Před 4 lety +1

    Congratulations my friend, that was a very good tutorial

  • @fleckenfurz77
    @fleckenfurz77 Před 3 lety

    try this in your browser console and see the difference:
    var obj = {
    checkThis: () => {
    console.log(this);
    }
    }
    obj.checkThis();
    and in comparison to that:
    var obj = {
    checkThis: function() {
    console.log(this)
    }
    }
    obj.checkThis();

  • @prisinha
    @prisinha Před 2 lety

    8:00 third approach of event binding

  • @lathagm666
    @lathagm666 Před 10 měsíci

    completed video 14. Thank You : -)

  • @shivanipanchal8744
    @shivanipanchal8744 Před 4 lety

    I like your all videos. Thanku for making videos on React

  • @anoopvasudevan
    @anoopvasudevan Před 4 lety

    Super clear explanation!

  • @prisinha
    @prisinha Před 2 lety

    9:36 4th approach of event binding

  • @amlanmishra6273
    @amlanmishra6273 Před 2 lety +1

    After introduction of hooks in react, how much of a importance does 'this' keyword still has??

  • @bnsexpress5171
    @bnsexpress5171 Před 3 lety

    excellent teaching

  • @segunolamide1989
    @segunolamide1989 Před 11 měsíci

    Short course but on point.

  • @faruzzy
    @faruzzy Před 5 lety +2

    This is gold!!
    Thanks!

  • @haciendadad
    @haciendadad Před 2 lety

    Excellent explanation!

  • @divyapawar6269
    @divyapawar6269 Před 2 lety +5

    The tutorial are good but most of them are based on classes. And now classes are out of function....

  • @juliocesarbenavente9590
    @juliocesarbenavente9590 Před 4 lety +1

    I understood why 'this' keyword doesn't work, I don't know if you explained it but I don't get why 'bind(this)' works.
    Thanks for the work!

  • @vaibsify
    @vaibsify Před 2 lety

    awesome explanation 🙏

  • @that_marathi_guy
    @that_marathi_guy Před 4 lety

    HI Vishwas,
    So far i was able to follow you in tutorial. In this Video , i have dpubt as, we have used button event to change message and increment value in previous tutorials . How that function invocation different in here from Event calling?

  • @kiattisakphanphu3823
    @kiattisakphanphu3823 Před 4 lety

    perfect course , thank you , i'm from thailand

  • @FoodYogawithRajeesha
    @FoodYogawithRajeesha Před 3 lety

    Very good tutorials

  • @medhapatil1372
    @medhapatil1372 Před 5 lety +1

    Thank you so much! Great Tutorial.

  • @tomasruzicka9835
    @tomasruzicka9835 Před 3 lety +1

    If i wasn't familiar with JS this keyword and prototyping I'd be confused as swarming forest bee.

  • @ignand
    @ignand Před 4 lety +1

    thank you!

  • @cesarsantana1097
    @cesarsantana1097 Před 3 lety +1

    Thank you a lot!

  • @judewestburner
    @judewestburner Před 2 lety

    Approach number 4 is easily the cleanest

  • @pavelbukhteev9062
    @pavelbukhteev9062 Před 4 lety

    Thank you very much! This course it really useful. I have a question: if I want to change the message from state to state by clicking, what should I do?

  • @nikolavladimirov1254
    @nikolavladimirov1254 Před 3 lety +1

    Thanks man!!!

  • @codeblooded6760
    @codeblooded6760 Před 4 lety

    Whats the difference between normal function and arrow function made in the 4th method. Plz explain

  • @sou123456100
    @sou123456100 Před rokem

    This is really very helpfull tutorial

  • @nassimback
    @nassimback Před 2 lety

    I prefer the 4th option, is it still an experimental feature ?

  • @ellsonmds5310
    @ellsonmds5310 Před 4 lety +1

    great tutorial, thnx!

  • @prateeksaha6287
    @prateeksaha6287 Před 3 lety

    i liked ur explanation , thanx a lot man