-
-
Save haykadamyan/216fb8b2ab27e33af1ced2065da947ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import moment from 'moment'; | |
import styles from './component.pcss'; | |
class Countdown extends React.Component { | |
interval = null; | |
componentDidMount() { | |
this.interval = setInterval(() => this.setState({ time: Date.now() }), 1000); | |
} | |
componentWillUnmount() { | |
clearInterval(this.interval); | |
} | |
render() { | |
const { startedAt } = this.props; | |
const now = moment(); | |
const nowSeconds = now.seconds(); | |
const startedAtSeconds = startedAt.seconds(); | |
const dif = nowSeconds - startedAtSeconds; | |
const minutes = parseInt(moment.duration(startedAt.diff(now)).asMinutes()); | |
const seconds = dif > 0 ? 60 - dif : Math.abs(dif); | |
return ( | |
<div className={styles.root}> | |
<p> | |
{ | |
`${minutes}:${seconds}` | |
} | |
</p> | |
</div> | |
); | |
} | |
} | |
export default Countdown; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment