Created
October 13, 2016 20:04
-
-
Save markshust/05398db211f1b653d4ff1e7e8d3e08c9 to your computer and use it in GitHub Desktop.
check if react component is mounted using recompose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How about several instances of the same component?
I suppose, all instances will use the same variable.