Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3.  
  4.  
  5. class Counter extends React.Component{
  6. constructor(props) {
  7. super(props);
  8.  
  9. this.state = {
  10. sec: 0,
  11. date: new Date(),
  12. info: 'super zegar!'
  13. }
  14.  
  15. console.log('constructor');
  16. }
  17.  
  18. componentWillMount() {
  19. console.log('componentWillMount')
  20. }
  21.  
  22. componentDidMount(){
  23. this.id = setInterval(() => {
  24.  
  25.  
  26. this.setState({
  27. sec: this.state.sec +1,
  28. date: new Date(),
  29. });
  30.  
  31. console.log('interval!');
  32. }, 1000);
  33.  
  34. console.log('componentDidMoiunt');
  35. }
  36.  
  37. componentWillUnmount() {
  38. //alert('unmount!!!');
  39. clearInterval(this.id);
  40. console.log('componentWillUnmount');
  41. }
  42.  
  43. render(){
  44. console.log('render');
  45. return <p><span>{this.state.info}</span>: Minęło <strong>{this.state.sec}</strong> sekund, obecnie mamy: <span>{this.state.date.toLocaleString()}</span></p>;
  46. }
  47.  
  48. componentDidUpdate() {
  49. console.log('componentDidUpdate')
  50. }
  51. }
  52.  
  53. class App extends React.Component {
  54. constructor(props) {
  55. super(props);
  56.  
  57. this.state = {
  58. showTimer: true,
  59. }
  60. }
  61.  
  62. componentDidMount() {
  63. this.timeout = setTimeout(() => {
  64. this.setState({
  65. showTimer: false,
  66. })
  67. }, 5000);
  68. }
  69.  
  70. componentWillUnmount() {
  71. clearTimeout(this.timeout);
  72. }
  73.  
  74. render() {
  75.  
  76. if(this.state.showTimer) {
  77. return <Counter />
  78. }
  79.  
  80. return <p>Koniec życia dla Counter</p>
  81. }
  82. }
  83.  
  84. document.addEventListener('DOMContentLoaded', function(){
  85. ReactDOM.render(
  86. <App/>,
  87. document.getElementById('app')
  88. );
  89. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement