Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3.  
  4. class StrobeLight extends React.Component {
  5. constructor(props) {
  6. super(props);
  7.  
  8. this.state = {
  9. color: this.props.color,
  10. }
  11. }
  12.  
  13. componentDidMount() {
  14. this.interval = setInterval(() => {
  15. this.setState({
  16. color: this.state.color === 'white' ? this.props.color : 'white',
  17. })
  18. }, this.props.frequency);
  19. }
  20.  
  21. componentWillUnmount() {
  22. clearInterval(this.interval);
  23. }
  24.  
  25. render() {
  26. const style = {
  27. height: '50px',
  28. width: '500px',
  29. backgroundColor: this.state.color,
  30. margin: '10px'
  31. }
  32.  
  33. return <div style={ style }></div>
  34. }
  35. }
  36.  
  37. class App extends React.Component {
  38. render() {
  39. return (
  40. <div>
  41. <StrobeLight color="red" frequency={ 150 } />
  42. <StrobeLight color="blue" frequency={ 250 } />
  43. <StrobeLight color="green" frequency={ 50 } />
  44. </div>
  45. )
  46. }
  47. }
  48.  
  49. document.addEventListener('DOMContentLoaded', function(){
  50. ReactDOM.render(
  51. <App/>,
  52. document.getElementById('app')
  53. );
  54. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement