Guest User

Untitled

a guest
May 26th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. import React from 'react';
  2. import { render } from 'react-dom';
  3. import Hello from './Hello';
  4.  
  5. const styles = {
  6. fontFamily: 'sans-serif',
  7. textAlign: 'center',
  8. };
  9.  
  10. class AutoDestroy extends React.Component {
  11. state = {
  12. destroyed: false
  13. }
  14.  
  15. componentDidMount() {
  16. setTimeout(
  17. () => this.setState({ destroyed: true }),
  18. this.props.millis,
  19. )
  20. }
  21.  
  22. render() {
  23. return this.state.destroyed ? null : this.props.children;
  24. }
  25. }
  26.  
  27. const withAutoDestroy = (millis, Component) => ({...props}) => (
  28. <AutoDestroy millis={millis}>
  29. <Component {...props} />
  30. </AutoDestroy>
  31. )
  32.  
  33. const App = withAutoDestroy(3000, () => (
  34. <div style={styles}>
  35. <Hello name="CodeSandbox" />
  36. <AutoDestroy millis={2000}>
  37. <h2>Start editing to see some magic happen {'\u2728'}</h2>
  38. </AutoDestroy>
  39. </div>
  40. ));
  41.  
  42. render(<App />, document.getElementById('root'));
Add Comment
Please, Sign In to add comment