Skip to content

useAsync with deferFn calls run with stale props if component rerendering #69

Closed
@Hianz

Description

@Hianz

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));

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions