reactjs / rfcs

RFCs for changes to React

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Why not change useCallback to a more useful thingy?

ruifortes opened this issue · comments

Has useCallback is simple sugar for "useMemo(() => (...args) => doSomething..." why not make useCallback more useful and at the same time identify a new concept (sometimes called useEventCallback)

export default function useEventCallback(cb, dependencies = []) {
  const ref = useRef(() => {
    throw new Error('Cannot call an event handler while rendering.')
  })

  useLayoutEffect(() => {
    ref.current = cb
  }, [cb, ...dependencies])

  return useMemo(() => (...args) => ref.current(...args) , [ref])
}

I know this one's also inconsequential (but not my post about allowing to skip the last hooks) but at least the core react hooks would help address important different concepts instead of just simple sugar coating

Can you provide an example of when this can be useful?

commented

Hi, thanks for your suggestion. RFCs should be submitted as pull requests, not issues. I will close this issue but feel free to resubmit in the PR format.