title: useCallback with dependency vs using a ref to call the last version of the function
body: <p>While doing a code review, I came across this custom hook:</p>
  <pre class="lang-js prettyprint-override"><code>import { useRef, useEffect, useCallback } from 'react'
  
  export default function useLastVersion (func) {
    const ref = useRef()
    useEffect(() =&gt; {
      ref.current = func
    }, [func])
    return useCallback((...args) =&gt; {
      return ref.current(...args)
    }, [])
  }
  </code></pre>
  <p>This hook is used like this:</p>
  <pre class="lang-js prettyprint-override"><code>const f = useLastVersion(() =&gt; { // do stuff and depends on props })
  </code></pre>
  <p>Basically, compared to <code>const f = useCallBack(() =&gt; { // do stuff }, [dep1, dep2])</code> this avoids to declare the list of dependencies and <code>f</code> never changes, even if one of the dependency changes.</p>
  <p>I don't know what to think about this code. I don't understand what are the disadvantages of using <code>useLastVersion</code> compared to <code>useCallback</code>.</p>