Skip to content

Instantly share code, notes, and snippets.

@markshust
Created October 13, 2016 20:04
Show Gist options
  • Save markshust/05398db211f1b653d4ff1e7e8d3e08c9 to your computer and use it in GitHub Desktop.
Save markshust/05398db211f1b653d4ff1e7e8d3e08c9 to your computer and use it in GitHub Desktop.
check if react component is mounted using recompose
import { compose, lifecycle, mapProps, withState } from 'recompose';
import MyComponent from '../components/MyComponent';
let isMounted = false;
const enhance = compose(
lifecycle({
componentDidMount() {
isMounted = true;
},
componentWillUnmount() {
isMounted = false;
},
}),
withState('error', 'setError', ''),
mapProps(({
setError,
...state,
}) => ({
onSubmit: (e) => {
e.preventDefault();
// Remote method goes here...
HTTP.get('/', (err, res) => {
if (isMounted && error) setError(err);
});
},
...state,
})),
);
export default enhance(MyComponent);
@dmitryt
Copy link

dmitryt commented Nov 25, 2018

How about several instances of the same component?
I suppose, all instances will use the same variable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment