JavaScript Please explain simply when/why I need to use bind() please simply explain

When would I do <CustomComponent function = {this.FunctionName}/>

instead of <CustomComponent function = {this.FunctionName.bind(this)}?

React's documentation mentions that bind() solves this problematic inequality: obj.method(); =/= var method = obj.method; method();. In what way would the second call to method be different from the first? Would it not have access to the instance vars, etc. of the instance of the object it's being called from? Also, what exactly are the arguments in a bind() - namely, is this.FunctionName.bind(this) binding to FunctionName?

Answer:1

Simple experiment to understand this:
Make 2 Js files. In first file, define a variable with some value and print it. In second file, change the value of that variable and print again.

File 1:

let x = 0;  // variable declared here
console.log(x);

File 2:

x = 2;  // no variable x declared just value change
console.log(x); 

Now when you include both files in your HTML page (File 1 before File 2), the output is:

0  
2 

Conclusion: You can access variable in one file from another file on a single page. The variables are common for a page !

Now consider defining something in ReactJs class or method component. Since React is Js library only, you would be able to access functions inside the class without an object of the class ! This will defeat the purpose of abstraction and encapsulation. So, what bind keyword does is, it tells that this function is bound to this class only and cannot be accessed outside it. That is why it is important to bind everything to your component. Otherwise two components using same function name might get confused which function to actually call if you don't bind them.

So, first time when you bind them, use the bind keyword and then use it without bind. Or, you can simply create arrow functions which will auto bind them to the component.

Answer:2

Whenever you create a new function (Except an arrow function) you create it's own instance of this. So inside it you can't use the scope of parent's this

Bind creates a new function that will have this set to the first parameter passed to bind().

So if here <CustomComponent function = {this.FunctionName.bind(this)} You pass parent's this.

This is done so that you can reference the state inside a function. If you don't want to use bind then you can just make your functions arrow functions.

Answer:3

This isn't really about React - just standard JS behavior of this. You should check out a tutorial on it, but in short, in the first side of the inequality, any uses of this will evaluate to obj; in the second case, this might evaluate to anything; for example, if it's called in the global scope, window.

The first argument to bind sets the this object for that instance of this.functionName. If you don't set it explicitly like this, then its internal this will look up the prototype chain in the second case to find obj (or not).

Answer:4

I have a small issue with a recursive .find() . when I use it to find ID === 2, it returns be the object with ID === 1 ... I have been scratching my head and I don't understand why that happens my ...

I have a small issue with a recursive .find() . when I use it to find ID === 2, it returns be the object with ID === 1 ... I have been scratching my head and I don't understand why that happens my ...

I'm learning JS/Jquery and I'm trying to make a part of a form show and hide when clicking on part of the form. I am able to open part of the form, but not to close it again. As there is no error ...

I'm learning JS/Jquery and I'm trying to make a part of a form show and hide when clicking on part of the form. I am able to open part of the form, but not to close it again. As there is no error ...

  1. show hide part of text jquery
  2. css-only hide/show part of table
  3. show hide part of div

I'm trying to correctly orient an image uploaded to a canvas from Apple devices. Here's a sample of my code to correctly orient something with EXIF orientation 6 ctx.rotate(0.5 * Math.PI); ctx....

I'm trying to correctly orient an image uploaded to a canvas from Apple devices. Here's a sample of my code to correctly orient something with EXIF orientation 6 ctx.rotate(0.5 * Math.PI); ctx....

  1. canvas rotate translate
  2. canvas rotate transform
  3. js canvas rotate translate
  4. canvas scale rotate translate
  5. html canvas translate rotate
  6. javascript canvas translate rotate

I am trying to convert form data into json I have a html table as following. On form submit what i want to do is convert it to json <form class="sales-order-form"> <table class="table"&...

I am trying to convert form data into json I have a html table as following. On form submit what i want to do is convert it to json <form class="sales-order-form"> <table class="table"&...