score:0

generally you would make use of inline arrow functions when you need to bind he handler to the context or to provide custom parameters

in

<char click={()=>this.ondeletehandler(index)}/>

ondeletehandler is bound to the context where char is rendered and is passed a custom parameter index. since a new function is returned to click , it can be executed from within char like this.props.click()

<char click={this.ondeletehandler()}/>

here the ondeletehandler is evaluated and the value is returned to the click prop

<person click={changed={(event) => this.namechangedhandler(event, person.id)} />

here the syntax is invalid, it should probably be

<person changed={(event) => this.namechangedhandler(event, person.id)} />

in which case, it takes the default parameter and pass it along with the custom parameter to namechangedhandler and it also performs binding

<char click={this.ondeletehandler}/>

just assigns the reference of ondeletehandler to click and whenever you invoke click, ondeletehandler will be called with the parameters that you pass while invoking click and the context within ondeletehandler will refer to the context from where it is invoked unless you bind ondeletehandler using arrow function or in constructor

score:1

the whole question seems to boil down to what the difference between func and func() and () => func() is. it has nothing to do specifically with react.

if i have a function

function func() {
  console.log(42);
}

then i can reference the function object itself via func. this is useful if i need to pass the function to another function, e.g. settimeout:

settimeout(func, 1000); // calls func after 1000ms

settimeout expects a function that it can call after the provided timeout.

similarly in react, click, change etc are all props that expect to be passed a function that is called when the event happens.


func() on the other hand calls the function. this needs to be done if you actually need to call function right then and there. this implies that if i do

settimeout(func(), 1000);

then i would call func first and pass its return value to settimeout (i.e. i call func now, settimeout doesn't call it later). so this is usually incorrect unless func returns a function itself and its really the return value i want to pass to the other function.


() => func() is just a new function that only calls func. for all intends and purposes it is equivalent to func:

function func() {
  console.log(42);
}

const anotherfunc = () => func();

func();
anotherfunc();

and of course if func expects an argument then i have to pass it along when wrapping it in another function, which is what x => func(x) does.


the other part is how functions assigned to object properties and this work. in short, what this refers to inside a (non-arrow) function depends on how the function is called. doing

this.foo();
// or
var bar = this.foo;
bar();

produces two different results because this.foo() and bar() are two different ways to call the function. for more info see how to access the correct `this` inside a callback?

score:7

<char click={()=>this.ondeletehandler(index)}/>

it passes anonymous function as a callback which - when clicked - triggers ondeletehandler with extra index parameter (which has to be defined in the scope before).

<char click={this.ondeletehandler()}/>

it passes result of ondeletehandler() as a callback - probably a bad idea - ondeletehandler function has to return another function that will be invoked on click.

<person click={changed={(event) => this.namechangedhandler(event, person.id)} />

looks invalid - will result with syntax error.

<char click={this.ondeletehandler}/>

similar to the first example but doesn't take custom parameters. default click event will be passed as a first argument for ondeletehandler


Related Query

More Query from same tag