question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Deprecate `isMounted`

See original GitHub issue

isMounted is already unavailable on ES6 classes, and we already have a warning saying we “might” remove them, but we don’t actually have a github issue to deprecate them. As per our discussions today, we are basically agreed that we’re going to start moving away from isMounted and deprecate it. We still need to figure out some good stories around promises (and related use cases).

This issue is to track progress toward that goal.

For background, please read:

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:4
  • Comments:50 (18 by maintainers)

github_iconTop GitHub Comments

294reactions
istarkovcommented, Mar 1, 2016

This simple method can be used to add cancel to any promise

const makeCancelable = (promise) => {
  let hasCanceled_ = false;

  const wrappedPromise = new Promise((resolve, reject) => {
    promise.then((val) =>
      hasCanceled_ ? reject({isCanceled: true}) : resolve(val)
    );
    promise.catch((error) =>
      hasCanceled_ ? reject({isCanceled: true}) : reject(error)
    );
  });

  return {
    promise: wrappedPromise,
    cancel() {
      hasCanceled_ = true;
    },
  };
};

EDIT: Updated for correctness/completeness.

HOW TO USE

const somePromise = new Promise(r => setTimeout(r, 1000));

const cancelable = makeCancelable(somePromise);

cancelable
  .promise
  .then(() => console.log('resolved'))
  .catch(({isCanceled, ...error}) => console.log('isCanceled', isCanceled));

// Cancel promise
cancelable.cancel();
21reactions
probablyupcommented, Nov 18, 2015

Listing ways to sup-up ES6 promises to make them cancellable is besides the point. The intent should be to provide a solution that works WITH the spec rather than trying to work AROUND the spec.

Read more comments on GitHub >

github_iconTop Results From Across the Web

isMounted is an Antipattern – React Blog
As we move closer to officially deprecating isMounted, it's worth understanding why the function is an antipattern, and how to write code ...
Read more >
FixedDataTable: isMounted is deprecated - Stack Overflow
What I've understood is that isMounted is seen as an antipattern Link so im suprised to see it in the actual source code....
Read more >
Creating an isMounted hook with useEffect (ReactJS)
Although isMounted is deprecated in ReactJS, maybe you need it to prevent memory leak errors in your application. Learn why there are better ......
Read more >
Writing a React hook to cancel promises when a component ...
Thus isMounted() is being deprecated. They recommend you write cancelable promise. A recipe is provided in the same page based on this. It...
Read more >
Should You Stop Using isMounted in React? | by Bikash Paneru
If we were to remove the if (this.isMounted) return; guard clause, we would get a warning from React if the component was not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found