Skip to content

Instantly share code, notes, and snippets.

@jgcmarins
Created May 21, 2018 22:00
Show Gist options
  • Save jgcmarins/2fcd78bcaf581f3fdc3e206f332676e5 to your computer and use it in GitHub Desktop.
Save jgcmarins/2fcd78bcaf581f3fdc3e206f332676e5 to your computer and use it in GitHub Desktop.
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