Seems to me useCallback could have benefitted from some additional Hammock time as well.
// useCallback avoids the changing props value// issue of newly created functions that don't behave// differently - but is doesn't address creating garbage// (i.e. unnecessary) functions on the majority of calls//conststepDelta=useCallback(()=>setDelta(incDelta),[]);conststepCount=useCallback(()=>setCount(count=>count+delta),[delta]);
In terms of design I would have expected something more along these lines:
// Component PartsconstinitialDelta=1;constincDelta=delta=>delta+1;constrefreshDeltaCallback=([setDelta])=>{console.log('refresh delta callback');return()=>setDelta(incDelta);};constinitialCount=0;constrefreshCountCallback=([delta,setCount])=>{console.log('refresh count callback with delta:',delta);constincCount=count=>count+delta;return()=>setCount(incCount);};// ...// Component// contrived use of useRefreshCallback - "cost of checks" yada, yadafunctionApp(){const[delta,setDelta]=useState(initialDelta);const[count,setCount]=useState(initialCount);// call `refreshCallback` function only if dependencies changeconststepDelta=useRefreshCallback(refreshDeltaCallback,depsEqual,[setDelta]);conststepCount=useRefreshCallback(refreshCountCallback,depsEqual,[delta,setCount]);
Seems to me
useCallback
could have benefitted from some additional Hammock time as well.In terms of design I would have expected something more along these lines:
Example usage:
Gist: useRefreshCallback - custom hook that also avoids creating garbage functions.