Socket
Book a DemoInstallSign in
Socket

react-moment-countdown

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-moment-countdown

An unopinionated countdown timer component for React and moment.

0.4.2
latest
Source
npmnpm
Version published
Weekly downloads
416
-30.32%
Maintainers
1
Weekly downloads
 
Created
Source

react-moment-countdown

An unopinionated countdown component for React using moment.js.

Installation

$ npm install react-moment-countdown --save

How to use

Insert javascript Date instance into toDate props:

   import React from 'react';
   import ReactMomentCountDown from 'react-moment-countdown';

   export default function CountDownComponent () {
     const dateInFuture = new Date('2017-12-31');

     return (
       <ReactMomentCountDown toDate={dateInFuture} />
     );
   };

Insert moment instance into toDate props:

import React from 'react';
import ReactMomentCountDown from 'react-moment-countdown';
import moment from 'moment';

export default function CountDownComponent () {
  const dateInFuture = moment('2017-12-31', 'YYYY-MM-DD');

  return (
    <ReactMomentCountDown toDate={dateInFuture} />
  );
};

Insert string value into toDate props with additional sourceFormatMask props:

The default value of sourceFormatMask props is YYYY-MM-DD.

import {render} from 'react-dom'
import ReactMomentCountDown from 'react-moment-countdown'

render(
  <ReactMomentCountDown toDate='2017-12-31 23:59:59' sourceFormatMask='YYYY-MM-DD HH:mm:ss' />,
  document.getElementById('sample-date')
)

Customize

targetFormatMask props

You can customize countdown format display with targetFormatMask props (default is 'HH:mm:ss'), for example

import {render} from 'react-dom'
import ReactMomentCountDown from 'react-moment-countdown'
import moment from 'moment'

const sampleDate = moment('2017-12-31')

render(
  <ReactMomentCountDown toDate={sampleDate} targetFormatMask='DD:HH:mm:ss' />,
  document.getElementById('sample-date')
)

onTick and onCountdownEnd props

You can add onTick function to trigger actions for every tick and onCountdownEnd to trigger actions when countdown timer has ended

import React, {PureComponent} from 'react'
import ReactMomentCountDown from 'react-moment-countdown'
import moment from 'moment'

export default class SampleClass extends PureComponent {
  constructor (props) {
    super(props)
    this.state = { endCountdown: false }
    this.sampleOnTick = this.sampleOnTick.bind(this)
    this.sampleOnCountdownEnd = this.sampleOnCountdownEnd.bind(this)
  }
  sampleOnTick (countdown) {
    this.setState({ endCountdown: countdown <= 0 })
  }
  sampleOnCountdownEnd () {
    console.log('Happy Birthday to you :)')
  }
  render () {
    const dateInFuture = moment('2017-12-31', 'YYYY-MM-DD');
    return (
      <ReactMomentCountDown toDate={dateInFuture}
        onTick={this.sampleOnTick}
        onCountdownEnd={this.sampleOnCountdownEnd} />
    )
  }
}

TO-DO

  • Add unit test
  • Add a Storybook for examples and UI tests

Keywords

react

FAQs

Package last updated on 18 Nov 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.