Created
May 21, 2018 22:00
-
-
Save jgcmarins/2fcd78bcaf581f3fdc3e206f332676e5 to your computer and use it in GitHub Desktop.
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 React from 'react'; | |
import { render } from 'react-dom'; | |
import Hello from './Hello'; | |
const styles = { | |
fontFamily: 'sans-serif', | |
textAlign: 'center', | |
}; | |
class AutoDestroy extends React.Component { | |
state = { | |
destroyed: false | |
} | |
componentDidMount() { | |
setTimeout( | |
() => this.setState({ destroyed: true }), | |
this.props.millis, | |
) | |
} | |
render() { | |
return this.state.destroyed ? null : this.props.children; | |
} | |
} | |
const withAutoDestroy = (millis, Component) => ({...props}) => ( | |
<AutoDestroy millis={millis}> | |
<Component {...props} /> | |
</AutoDestroy> | |
) | |
const App = withAutoDestroy(3000, () => ( | |
<div style={styles}> | |
<Hello name="CodeSandbox" /> | |
<AutoDestroy millis={2000}> | |
<h2>Start editing to see some magic happen {'\u2728'}</h2> | |
</AutoDestroy> | |
</div> | |
)); | |
render(<App />, document.getElementById('root')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment