0

Could you please explain this JS code chunk:

const fn = (order, active, originalIndex, curIndex, y) => (index) => {

//...

  }

As far as I understood, fn takes 5 arguments, but from where does it take "index"?

Later on, fn is called 2 times:

fn(order.current)

fn(newOrder, active, originalIndex, curIndex, y) 

The whole snippet is here: https://codesandbox.io/embed/r5qmj8m6lq

Thanks a million!

ZenBerry
  • 13
  • 2
  • *"but from where does it take "index"?"* *It* doesn't take `index`. `fn` is a function that *returns a function*. The caller that calls the return functions provides a value for `index`. – Felix Kling Feb 06 '21 at 14:30
  • The syntax means that: You have funciton `fn()`. The return value of `fn` is **a function** - which means that you can **invoke** the result of it. Example: `fn(...parameters...)(index)` – Ravid Feb 06 '21 at 14:33
  • Dear Felix, could you please clarify how exactly the caller provides a value for index? There are two calls: `setSprings(fn(newOrder, active, originalIndex, curIndex, y))` and `const [springs, setSprings] = useSprings(items.length, fn(order.current))` Thank you! – ZenBerry Feb 06 '21 at 15:12
  • I assume `fn(order.current)` is calling the returned function, so that's where it would pass in the index. – Felix Kling Feb 07 '21 at 09:23
  • Thank you so much! You're right, by console.logging I've found out that `fn(order.current)` is indeed calling the return function instead of the fn itself, and passes two arguments there. But could you please help to find out where might this behaviour be defined? Probably in the UseSprings hook? I'm just trying to avoid the confusion in the future) Bless you! – ZenBerry Feb 07 '21 at 15:10

0 Answers0