Description
If my deferFn
is defined outside of my component (which uses useAsync), the props
parameter is stale after the second render of the Child component if the props
of the Parent component change.
If the deferFn
is declared inside the component everything works as expected.
Here is a codeSandbox that shows the issue:
https://codesandbox.io/s/react-async-stale-props-lq141
(Clicking 2 times of 'Click me twice' & after that on the Start run()
button, the console will show the stale props
.)
Interestingly enough if i change the options
parameter inside useAsync.js
to prevOptions.current
everything works as expected. After the second render if the Parent props
change, options
has stale props
from the previous render and prevOptions.current
has the correct ones.
//options -> stale, prevOptions.current -> correct ones
return start(() => deferFn(args, options, abortController.current)).then(handleResolve(counter.current), handleReject(counter.current));