isMounted is an Antipattern

December 16, 2015 by Jim Sproch


As we move closer to officially deprecating isMounted, it's worth understanding why the function is an antipattern, and how to write code without the isMounted function.

The primary use case for isMounted() is to avoid calling setState() after a component has unmounted, because calling setState() after a component has unmounted will emit a warning. The “setState warning” exists to help you catch bugs, because calling setState() on an unmounted component is an indication that your app/component has somehow failed to clean up properly. Specifically, calling setState() in an unmounted component means that your app is still holding a reference to the component after the component has been unmounted - which often indicates a memory leak!

To avoid the error message, people often add lines like this:

if(this.isMounted()) { // This is bad.
  this.setState({...});
}

Checking isMounted before calling setState() does eliminate the warning, but it also defeats the purpose of the warning, since now you will never get the warning (even when you should!)

Other uses of isMounted() are similarly erroneous; using isMounted() is a code smell because the only reason you would check is because you think you might be holding a reference after the component has unmounted.

An easy migration strategy for anyone upgrading their code to avoid isMounted() is to track the mounted status yourself. Just set a _isMounted property to true in componentDidMount and set it to false in componentWillUnmount, and use this variable to check your component's status.

An optimal solution would be to find places where setState() might be called after a component has unmounted, and fix them. Such situations most commonly occur due to callbacks, when a component is waiting for some data and gets unmounted before the data arrives. Ideally, any callbacks should be canceled in componentWillUnmount, prior to unmounting.

For instance, if you are using a Flux store in your component, you must unsubscribe in componentWillUnmount:

class MyComponent extends React.Component {
  componentDidMount() {
    mydatastore.subscribe(this);
  }
  render() {
    ...
  }
  componentWillUnmount() {
    mydatastore.unsubscribe(this);
  }
}

If you use ES6 promises, you may need to wrap your promise in order to make it cancelable.

const cancelablePromise = makeCancelable(
  new Promise(r => component.setState({...}}))
);

cancelablePromise
  .promise
  .then(() => console.log('resolved'))
  .catch((reason) => console.log('isCanceled', reason.isCanceled));

cancelablePromise.cancel(); // Cancel the promise

Where makeCancelable is defined by @istarkov as:

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;
    },
  };
};

As an added bonus for getting your code cleaned up early, getting rid of isMounted() makes it one step easier for you to upgrade to ES6 classes, where using isMounted() is already prohibited. Happy coding!

React.js Conf 2016 Diversity Scholarship

December 4, 2015 by Paul O’Shannessy


I am thrilled to announced that we will be organizing another diversity scholarship program for the upcoming React.js Conf! The tech industry is suffering from a lack of diversity, but it's important to us that we have a thriving community that is made up of people with a variety of experiences and viewpoints.

When we ran this program last year, we had over 200 people apply for only 10 tickets. There were so many people that we wanted to bring in but we couldn't. The results were still awesome, and we had bright individuals from around the world attending who would have otherwise been unable to. These attendees took part in discussions at the conference and brought perspectives that we might not have otherwise seen there.

This year we're excited to bring back the scholarship, but we've set aside 40 tickets because we really believe that it's important to do our best to make sure we have an even more diverse audience.

This is something I'm personally really excited to be a part of. I know the rest of the team is as well. We're really proud to have everyone at Facebook providing support and funding for this.

The details of the scholarship are provided below (or you can go directly to the application). I encourage you to apply! If you don't feel like you are eligible yourself, you can still help – send this along to friends, family, coworkers, acquaintances, or anybody who might be interested. And even if you haven't spoken before, please consider submitting a proposal for a talk (either 30 minutes or just 5 minutes) - we're hoping to have a very diverse group of speakers in addition to attendees.


Facebook is excited to announce that we are now accepting applications for the React.js Conf Diversity Scholarship!

Beginning today, those studying or working in computer science or a related field can apply for a partial scholarship to attend the React.js Conf in San Francisco, CA on February 22 & 23, 2016.

React opens a world of new possibilities such as server-side rendering, real-time updates, different rendering targets like SVG and canvas. React Native makes is easy to use the same concepts and technologies to build native mobile experiences on iOS and Android. Join us at React.js Conf to shape the future of client-side applications! For more information about the React.js conference, please see the website.

At Facebook, we believe that anyone anywhere can make a positive impact by developing products to make the world more open and connected to the people and things they care about. Given the current realities of the tech industry and the lack of representation of communities we seek to serve, applicants currently under-represented in Computer Science and related fields are strongly encouraged to apply. Facebook will make determinations on scholarship recipients in its sole discretion. Facebook complies with all equal opportunity laws.

To apply for the scholarship, please visit the application page: http://goo.gl/forms/PEmKj8oUp4

Award Includes #

  • Paid registration fee for the React.js Conf Feburary 22 & 23 in downtown San Francisco, CA
  • Paid lodging expenses for February 21, 22, 23

Important Dates #

  • Sunday December 13th 2015 - 11:59 PST: Applications for the React.js Conf Scholarship must be submitted in full
  • Wednesday, December 16th, 2015: Award recipients will be notified by email of their acceptance
  • Monday & Tuesday, February 22 & 23, 2016: React.js Conf

Eligibility #

  • Must currently be studying or working in Computer Science or a related field
  • International applicants are welcome, but you will be responsible for securing your own visa to attend the conference
  • You must be able to provide your own transportation to San Francisco
  • You must be available to attend the full duration of React.js Conf on February 22 & 23 in San Francisco, CA

React v0.14.3

November 18, 2015 by Paul O’Shannessy


It's time for another installment of React patch releases! We didn't break anything in v0.14.2 but we do have a couple of other bugs we're fixing. The biggest change in this release is actually an addition of a new built file. We heard from a number of people that they still need the ability to use React to render to a string on the client. While the use cases are not common and there are other ways to achieve this, we decided that it's still valuable to support. So we're now building react-dom-server.js, which will be shipped to Bower and in the dist/ directory of the react-dom package on npm. This file works the same way as react-dom.js and therefore requires that the primary React build has already been included on the page.

The release is now available for download:

We've also published version 0.14.3 of the react, react-dom, and addons packages on npm and the react package on bower.


Changelog #

React DOM #

  • Added support for nonce attribute for <script> and <style> elements
  • Added support for reversed attribute for <ol> elements

React TestUtils Add-on #

  • Fixed bug with shallow rendering and function refs

React CSSTransitionGroup Add-on #

  • Fixed bug resulting in timeouts firing incorrectly when mounting and unmounting rapidly

React on Bower #

  • Added react-dom-server.js to expose renderToString and renderToStaticMarkup for usage in the browser

React v0.14.2

November 2, 2015 by Paul O’Shannessy


We have a quick update following the release of 0.14.1 last week. It turns out we broke a couple things in the development build of React when using Internet Explorer. Luckily it was only the development build, so your production applications were unaffected. This release is mostly to address those issues. There is one notable change if consuming React from npm. For the react-dom package, we moved react from a regular dependency to a peer dependency. This will impact very few people as these two are typically installed together at the top level, but it will fix some issues with dependencies of installed components also using react as a peer dependency.

The release is now available for download:

We've also published version 0.14.2 of the react, react-dom, and addons packages on npm and the react package on bower.


Changelog #

React DOM #

  • Fixed bug with development build preventing events from firing in some versions of Internet Explorer & Edge
  • Fixed bug with development build when using es5-sham in older versions of Internet Explorer
  • Added support for integrity attribute
  • Fixed bug resulting in children prop being coerced to a string for custom elements, which was not the desired behavior.
  • Moved react from dependencies to peerDependencies to match expectations and align with react-addons-* packages

React v0.14.1

October 28, 2015 by Paul O’Shannessy


After a couple weeks of having more people use v0.14, we're ready to ship a patch release addressing a few issues. Thanks to everybody who has reported issues and written patches!

The release is now available for download:

We've also published version 0.14.1 of the react, react-dom, and addons packages on npm and the react package on bower.


Changelog #

React DOM #

  • Fixed bug where events wouldn't fire in old browsers when using React in development mode
  • Fixed bug preventing use of dangerouslySetInnerHTML with Closure Compiler Advanced mode
  • Added support for srcLang, default, and kind attributes for <track> elements
  • Added support for color attribute
  • Ensured legacy .props access on DOM nodes is updated on re-renders

React TestUtils Add-on #

  • Fixed scryRenderedDOMComponentsWithClass so it works with SVG

React CSSTransitionGroup Add-on #

  • Fix bug preventing 0 to be used as a timeout value

React on Bower #

  • Added react-dom.js to main to improve compatibility with tooling