According to this pageuseCallback(fn, inputs) is equivalent to useMemo(() => fn, inputs)
I suggest another one (implemented with current hooks):
functionuseHandler(handler){constholder=useRef();holder.current=handler;// TODO: avoid useMemo?returnuseMemo(()=>function(...args){returnholder.current.apply(this,args);},[]);}It has advantages:
- no need to specify
inputs, latest instance of handler will be called - result of
useHandler is constant for all rerenders
According to this page
useCallback(fn, inputs)is equivalent touseMemo(() => fn, inputs)I suggest another one (implemented with current hooks):
It has advantages:
inputs, latest instance ofhandlerwill be calleduseHandleris constant for all rerenders