1

I'm trying to understand every line of this beautiful example: https://codesandbox.io/embed/r5qmj8m6lq

Could you please suggest the possible reason to utilise useRef on line 18:

const order = useRef(items.map((_, index) => index))

if it could be replaced by

var order = items.map( (_, index) => index )

(and then of course we have to refer to order rather than to order.current)

Thanks a lot!

ZenBerry
  • 217
  • 2
  • 12
  • 1
    You actually asking about the difference between scoped variable and useRef, `var order` will be reinitialized on every render, hence if you make a change to `order`, it will reset on the next render, see the duplicate. – Dennis Vash Feb 23 '21 at 13:54
  • 1
    Also, you can read about the [`render()`](https://reactjs.org/docs/react-component.html#render) function, in function component, the function's body is the render function – Dennis Vash Feb 23 '21 at 13:57

1 Answers1

1

I guess the reason for useRef here is to have a container which manages mutable values without causing a rerender cycle on value change

Into Numbers
  • 923
  • 11
  • 19