GithubHelp home page GithubHelp logo

brooklinjazz / use-moment-countdown Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 1.0 3.3 MB

a react or react native hook to create countdown timers using moment.js

HTML 4.50% JavaScript 25.97% TypeScript 69.54%

use-moment-countdown's Introduction

useCountdown

check out my explanatory blog post here

a react hook for creating countdown timers with moment.js

NPM JavaScript Style Guide

Install dependencies

npm install --save moment

Install use-moment-countdown

npm install --save use-moment-countdown

Usage

timer set in minutes

import React from "react";

import { useCountdown } from "use-moment-countdown";

const App = () => {
  const { start, time } = useCountdown({ m: 10 });
  return <div onClick={start}>{time.format("hh:mm:ss")}</div>;
};

export default App;

timer set in hours

import React from "react";

import { useCountdown } from "use-moment-countdown";

const App = () => {
  const { start, time } = useCountdown({ h: 1 });
  return <div onClick={start}>{time.format("hh:mm:ss")}</div>;
};

export default App;

timer set in seconds

import React from "react";

import { useCountdown } from "use-moment-countdown";

const App = () => {
  const { start, time } = useCountdown({ s: 30 });
  return <div onClick={start}>{time.format("hh:mm:ss")}</div>;
};

export default App;

timer set in combination of hours, minutes, and seconds

import React from "react";

import { useCountdown } from "use-moment-countdown";

const App = () => {
  const { start, time } = useCountdown({ h: 1, m: 20, s: 30 });
  return <div onClick={start}>{time.format("hh:mm:ss")}</div>;
};

export default App;

recurring timer

import React from "react";

import { useCountdown } from "use-moment-countdown";

const App = () => {
  const { start, time } = useCountdown({ s: 30 }, {recurring: true});
  return <div onClick={start}>{time.format("hh:mm:ss")}</div>;
};

export default App;

onDone callback when timer ends

import React from "react";

import { useCountdown } from "use-moment-countdown";

const App = () => {
  const myOnDoneCallback = () => alert("Your timer has finished!")
  const { start, time } = useCountdown({ s: 30 }, {onDone: myOnDoneCallback});
  return <div onClick={start}>{time.format("hh:mm:ss")}</div>;
};

export default App;

This hook is created using create-react-hook.

use-moment-countdown's People

Contributors

brooklinjazz avatar

Stargazers

Scott Beeker avatar  avatar  avatar

Watchers

James Cloos avatar  avatar

Forkers

hlight

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.