Skip to content
This repository has been archived by the owner on Nov 16, 2022. It is now read-only.

Commit

Permalink
0.4.0 (#42)
Browse files Browse the repository at this point in the history
* Removing obsolete component

* Fixing interval control
  • Loading branch information
thiagozanetti authored Feb 27, 2020
1 parent 65ee0c3 commit 139abfe
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 81 deletions.
17 changes: 8 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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 {
Expand All @@ -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 (
<span {...otherProps}>{countdown}</span>
Expand Down
72 changes: 0 additions & 72 deletions src/index.jsx

This file was deleted.

0 comments on commit 139abfe

Please sign in to comment.