-5
let arr;
for (var i = 0; i < 4; i++) {
  arr.push(
    <ListItem key={i}> // native-base component
      <Button
        onPress={this.pick(curJob, i)}>
      </Button>
    </ListItem>
  )
}


render(){
  return (
    { arr }
  )
}

In this code, what is the difference between the two functions?

Function 1.

pick = (job,index) => {
  console.log(job);
  console.log(index);
}

Function 2.

pick = (job,index) => () => {
  console.log(job);
  console.log(index);
}

I found that function 2 works fine but function 1 returns error (maximum call stack size exceeded) I wonder what is the difference between the two functions and if function1 can be called call-back function.

Johan
  • 8,068
  • 1
  • 33
  • 46
Jae
  • 376
  • 2
  • 11

2 Answers2

0

The second one is invalid.

The prototype of an arrow functions is the following :

variableName = (arguments) => {
    Body
}

Your onPress should be : onPress = {() => this.pick(curJob,i)}>, otherwise, the function is called everytime a render happens, so always. With the () => before, you are telling the program to run this.pick only onPress

TBouder
  • 2,539
  • 1
  • 14
  • 29
0
 pick = (job,index) => () => {
                console.log(job);
                console.log(index);
           }

this is just function returning a function its equal to

nestedfunc(job, index) => {
  console.log(job);
  console.log(index);
}


pick = (job,index) => nestedfunc(job,index);
Aurangzaib Rana
  • 4,028
  • 1
  • 14
  • 23