0

Why Binding is'nt needed for the nep() to run in the image below, enter image description here

but is needed for the hanndleSubmit() and formSubmit() to work, and throws error(onclick) if binding is removed!

enter image description here

I searched for this and got to know that binding are needed if we want to pass functions or variables as props without loosing their context, but in both example, props are not passed on to another component.

  • [Please do not upload images of code/errors when asking a question](//meta.stackoverflow.com/q/285551). See also [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) from the help centre. – Phil Aug 11 '22 at 06:09
  • 1
    (oversimplified) `this` depends on how a function is called - by the way, in more modern javascript you can do `formSubmit = (event) => ..... etc` then you don't need to bind it – Jaromanda X Aug 11 '22 at 06:13
  • Thanks Phil, i did not know that, i was confused as to how to show people the error! – Shivang Shukla Aug 11 '22 at 06:15
  • 1
    you didn't show any error though! – Jaromanda X Aug 11 '22 at 06:15
  • Jaromanda X, so this has some special property for forms? if it so, can you point me to some docs? – Shivang Shukla Aug 11 '22 at 06:16
  • no, nothing to do with forms - it's to do with how `this` works in javascript - see the duplicate for a fully fleshed out answer – Jaromanda X Aug 11 '22 at 06:16
  • Also, the error runs off too fast to catch without event.preventDefault() , but it says "UnCaught" – Shivang Shukla Aug 11 '22 at 06:18

1 Answers1

1

When you want to use variables in the instance of a component like a state you must bind the instance on the method.

In the first example, you call the method, you did not bind it.

In the second example, you just bind the "formsubmit" method and it will fire when the button clicks.

it's different between clicked={method} and clicked={method()} the first one is binding and secound one is calling .

Arash Ghazi
  • 633
  • 8
  • 16
  • so, in the first example, i need not to bind it as its a method directly running, but in second ex. the method is to be called on a button click, thats why it needs binding? is this it? – Shivang Shukla Aug 11 '22 at 06:21