Search code examples
react-async

react-async v. 8.0.0 - using new promise prop w/ Typescript & optimistic updates


I just updated to the newest version of react-async (8.0.0) and the new promise prop is giving me some trouble.

I'm trying to use the promise prop after my useAsync hook, as described in the release notes, but I'm getting a runtime error "TypeError: Cannot read property 'then' of undefined".

const { data: result, run, setData, promise } = useAsync({
    promiseFn: getPhoto,
    deferFn: postPhoto,
    userId: userId
  });
  promise.then(res => setData(res));

Solution

  • This is because promise is only defined when the promiseFn is invoked. This means it will be undefined at least until the second render. Because this behavior is inconsistent with the TS typings and unintuitive, it will be fixed in the next release.