0

I am learning react from Bob Ziroll's tutorial and there is a code

<Die key={die.id} value={die.value} isHeld={die.isHeld} holdDice={() => holdDice(die.id)}/>

the part holdDice={() => holdDice(die.id)} works

but when i try holdDice={holdDice(die.id)} it doesn't work. I can't get the reasoning of this.

sude
  • 11
  • 1
  • 1
    Listeners want functions that they can call. In 1) you're assigning a function to the listener that calls `holdDice` when the event is fired. In 2) You're calling `holdDice` and assigning the _result_ of calling that function to the listener. – Andy Sep 01 '22 at 11:26
  • 1
    In more general javascript terms (I know, not even React!) it's the difference between immediately executing it or wrapping it for execution later. In this case, if you do `{holdDice(die.id)}` it get executed immediately and its value will inserted there instead of a method call. If you pass in `() => holdDice(die.id)`, it will execute it on render as it's a function. – somethinghere Sep 01 '22 at 11:26

0 Answers0