Skip to content

Commit

Permalink
Monkey patch isMounted
Browse files Browse the repository at this point in the history
  • Loading branch information
emmenko committed Mar 22, 2015
1 parent 790ca62 commit 993aaa3
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion scripts/utils/connectToStores.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@
import React from 'react';
import shallowEqual from 'react/lib/shallowEqual';

// FIXME: `isMounted` is deprecated in React 0.13
// but we need this otherwise we get a warning
//
// Warning: setState(...): Can only update a mounted or mounting component.
// This usually means you called setState() on an unmounted component.
// This is a no-op.
//
// Apparently this is because `handleStoresChanged` is called multiple times,
// even when the component is unmounted. Not sure how to fix this yet...
const isMounted = (component) => {
try {
React.findDOMNode(component);
return true;
} catch (e) {
return false;
}
};

export default (Component, stores, pickProps, getState) => {
class StoreConnector extends React.Component {
constructor(props) {
Expand Down Expand Up @@ -34,7 +52,9 @@ export default (Component, stores, pickProps, getState) => {
}

handleStoresChanged() {
this.setState(this.getStateFromStores(this.props));
if (isMounted(this)) {
this.setState(this.getStateFromStores(this.props));
}
}

render() {
Expand Down

0 comments on commit 993aaa3

Please sign in to comment.