Closed
Description
According to this page useCallback(fn, inputs)
is equivalent to useMemo(() => fn, inputs)
I suggest another one (implemented with current hooks):
function useHandler(handler) {
const holder = useRef();
holder.current = handler;
// TODO: avoid useMemo?
return useMemo(
() =>
function(...args) {
return holder.current.apply(this, args);
},
[]
);
}
It has advantages:
- no need to specify
inputs
, latest instance ofhandler
will be called - result of
useHandler
is constant for all rerenders
Metadata
Metadata
Assignees
Labels
No labels