11import { computed , observable } from 'mobx' ;
22import { observer } from 'mobx-react' ;
3- import { Component } from 'react' ;
3+ import { ObservedComponent , reaction } from 'mobx- react-helper ' ;
44
55import styles from './Hero.module.less' ;
66
@@ -10,14 +10,14 @@ export interface CountdownProps {
1010}
1111
1212@observer
13- export class Countdown extends Component < CountdownProps > {
13+ export class Countdown extends ObservedComponent < CountdownProps > {
1414 @observable
1515 accessor rest : number | null = null ;
1616
1717 private timer ?: number ;
1818
1919 private get target ( ) {
20- const { countdownTo } = this . props ;
20+ const { countdownTo } = this . observedProps ;
2121 const value = countdownTo ? new Date ( countdownTo ) . getTime ( ) : NaN ;
2222
2323 return Number . isFinite ( value ) ? value : NaN ;
@@ -42,35 +42,27 @@ export class Countdown extends Component<CountdownProps> {
4242 this . rest = Math . max ( 0 , this . target - Date . now ( ) ) ;
4343 } ;
4444
45+ @reaction ( ( _this : Countdown ) => _this . observedProps . countdownTo )
4546 componentDidMount ( ) {
46- if ( Number . isFinite ( this . target ) ) {
47- this . tick ( ) ;
48- this . timer = window . setInterval ( this . tick , 1000 ) ;
49- }
50- }
51-
52- componentDidUpdate ( prevProps : CountdownProps ) {
53- if ( prevProps . countdownTo !== this . props . countdownTo ) {
54- if ( this . timer ) {
55- window . clearInterval ( this . timer ) ;
56- this . timer = undefined ;
57- }
47+ super . componentDidMount ( ) ;
5848
59- this . rest = null ;
60-
61- if ( Number . isFinite ( this . target ) ) {
62- this . tick ( ) ;
63- this . timer = window . setInterval ( this . tick , 1000 ) ;
64- }
49+ if ( this . timer ) {
50+ window . clearInterval ( this . timer ) ;
51+ this . timer = undefined ;
6552 }
53+
54+ this . tick ( ) ;
55+ this . timer = window . setInterval ( this . tick , 1000 ) ;
6656 }
6757
6858 componentWillUnmount ( ) {
59+ super . componentWillUnmount ( ) ;
60+
6961 if ( this . timer ) window . clearInterval ( this . timer ) ;
7062 }
7163
7264 render ( ) {
73- const { unitLabels } = this . props ;
65+ const { unitLabels } = this . observedProps ;
7466 const { sections } = this ;
7567
7668 return (
0 commit comments