@fdaciuk/use-timer
TypeScript icon, indicating that this package has built-in type declarations

0.2.1 • Public • Published

use-timer

React Hook to easily create decremental timers

NPM JavaScript Style Guide

Install

npm install --save @fdaciuk/use-timer

If you are using yarn:

yarn add @fdaciuk/use-timer

Usage

import React, { useEffect } from 'react'
import useTimer from '@fdaciuk/use-timer'

function Example () {
  const { minutes, seconds, start } = useTimer('05:00')

  useEffect(() => {
    start()
  }, [start])

  return (
    <div>{minutes}:{seconds}</div>
  )
}

Complete example

import React, { useCallback, useEffect } from 'react'
import useTimer from '@fdaciuk/use-timer'

function Example () {
  const {
    minutes,
    seconds,
    start,
    pause,
    finished,
    reset,
    setTimer
  } = useTimer('05:00')

  const handleSubmit = useCallback((e) => {
    e.preventDefault()
    setTimer(e.target.elements.time.value)
  }, [setTimer])

  useEffect(() => {
    finished(() => {
      console.log('finished!')
    })
  }, [finished])

  return (
    <>
      <h1>{minutes}:{seconds}</h1>
      <button onClick={start}>Start!</button>
      <button onClick={pause}>Pause</button>
      <button onClick={reset}>Reset timer</button>

      <form onSubmit={handleSubmit}>
        <input type='text' name='time' />
        <button type='submit'>Set new timer</button>
      </form>

    </>
  )
}

Properties and methods

  • minutes (String): show left minutes in 2 or more digits
  • seconds (String): show left seconds in 2 or more digits
  • start (Function): function that should be executed when the timer is supposed to start counting
  • pause (Function): function to pause timer,
  • finished (Function): function that receives another function that will be executed when timer finishes counting
  • reset (Function): function to reset counter to it's initial time, passed to useTimer hook
  • setTimer (Function): function to set a new timer

License

MIT © fdaciuk

Readme

Keywords

none

Package Sidebar

Install

npm i @fdaciuk/use-timer

Weekly Downloads

1

Version

0.2.1

License

MIT

Unpacked Size

18.3 kB

Total Files

7

Last publish

Collaborators

  • fdaciuk