From 139abfef86fa694fb2a69dd609c76f730b9983e8 Mon Sep 17 00:00:00 2001 From: Thiago Zanetti Date: Thu, 27 Feb 2020 14:57:09 -0300 Subject: [PATCH] 0.4.0 (#42) * Removing obsolete component * Fixing interval control --- src/index.js | 17 ++++++------ src/index.jsx | 72 --------------------------------------------------- 2 files changed, 8 insertions(+), 81 deletions(-) delete mode 100644 src/index.jsx diff --git a/src/index.js b/src/index.js index 674f097..fa68e65 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ -import React, { useState, useEffect } from 'react'; +import React, { useState, useEffect, useRef } from 'react'; import { oneOfType, func, instanceOf, string } from 'prop-types'; import { momentObj } from 'react-moment-proptypes'; @@ -14,14 +14,14 @@ const ReactMomentCountDown = ({ ...otherProps }) => { const [countdown, setCountdown] = useState(null); - const [timer, setTimer] = useState(null); + const timer = useRef(); const tick = () => { const [delta, lastCountdown] = formatDate(toDate, targetFormatMask, sourceFormatMask); if (delta <= 0) { - window.clearInterval(timer); - setTimer(null); + clearInterval(timer.current); + timer.current = null; onCountdownEnd(); } else { @@ -31,14 +31,13 @@ const ReactMomentCountDown = ({ } }; - // componentDidMount + // componentDidMount, componentWillUmnount useEffect(() => { tick(); - setTimer(window.setInterval(tick(), 1000)); - }, []); + timer.current = setInterval(tick, 1000); - // componentWillUnmount - useEffect(() => () => window.clearInterval(timer), []); + return () => clearInterval(timer.current); + }, []); return ( {countdown} diff --git a/src/index.jsx b/src/index.jsx deleted file mode 100644 index d66e1c2..0000000 --- a/src/index.jsx +++ /dev/null @@ -1,72 +0,0 @@ -import React, { Component } from 'react' -import { oneOfType, func, instanceOf, string } from 'prop-types' -import { momentObj } from 'react-moment-proptypes' - -import formatDate from './format-date' - -class ReactMomentCountDown extends Component { - constructor(props) { - super(props) - - this.state = { - countdown: null - } - } - - componentDidMount() { - this.tick() - - this.timer = window.setInterval(this.tick.bind(this), 1000) - } - - componentWillUnmount() { - window.clearInterval(this.timer) - } - - tick() { - const { toDate, sourceFormatMask, targetFormatMask, onCountdownEnd, onTick } = this.props - const [delta, countdown] = formatDate(toDate, targetFormatMask, sourceFormatMask) - - if (delta <= 0) { - window.clearInterval(this.timer) - - if (onCountdownEnd) { - onCountdownEnd() - } - } - - this.setState({ - countdown - }) - - if (onTick) { - onTick(delta) - } - } - render() { - return ( - {this.state.countdown} - ) - } -}; - -ReactMomentCountDown.propTypes = { - toDate: oneOfType([ - momentObj, - instanceOf(Date), - string - ]).isRequired, - sourceFormatMask: string, - targetFormatMask: string, - onTick: func, - onCountdownEnd: func -} - -ReactMomentCountDown.defaultProps = { - sourceFormatMask: 'YYYY-MM-DD', - targetFormatMask: 'HH:mm:ss', - onTick: null, - onCountdownEnd: null -} - -export default ReactMomentCountDown