DEV Community

Cover image for Why do we need to bind methods inside our class component’s constructor?

Why do we need to bind methods inside our class component’s constructor?

Amandeep Singh on August 19, 2019

If we go back into the time where the hooks were not yet born, we will inevitably encounter this code while creating a React class component. cla...
Collapse
 
chrisachard profile image
Chris Achard

Yep! This is the reason I always recommend arrow functions :)

Collapse
 
mohamediyed profile image
mohamed-iyed

this article actually helped me a lot, thank you very much <3 !

Collapse
 
aman_singh profile image
Amandeep Singh

Thanks for your comment.

Collapse
 
luciferwalker profile image
Preet Patel

Thank you for this too good article! The explanation was smooth and the examples helped a lot! Almost all my doubts are clear.

Collapse
 
aman_singh profile image
Amandeep Singh

Glad it was helpful.

Collapse
 
karataev profile image
Eugene Karataev

Why React doesn't bind functions internally? It seems it's not that hard to implement this feature under the hood. Why do we need to mess around with bindings in the user land?

Collapse
 
remejuan profile image
Reme Le Hane

Well, seeing as this is javascript and not react, I would expect it not to. Classes as not unique to react so why should they work different in react from every other instance?

On top of that, doing any blackmagic auto bindings would more than likely end up with undesiderwd side effects which would result in "why IS react binding this internally?"

The idea behind react is to provide a highly performance dom manager while sticking as close to the core of javascript, you want black magic, that what angular is there for, to move as far away from the core language as possible.

Collapse
 
aman_singh profile image
Amandeep Singh

Thanks, Reme for your reply 😀. And I think this is one of the reasons I always suggest my other fellow friends (who are new to React) to strengthen their JavaScript fundamentals first. Doing so will help them align and understand how any framework works.

Collapse
 
zippcodder profile image
Deon Rich

Hum, that is strange. I mean their code structure is the reason for binding so why dont they just fix it and be done with it.

Collapse
 
oosharma profile image
Abhishek Sharma

This was great! Thanks!