1.0.3 • Published 5 years ago

simple-use-timer v1.0.3

Weekly downloads
-
License
ISC
Repository
-
Last release
5 years ago

simpleUseTimer

simpleUseTimer is a bare-bones React Hook that helps time events. It returns two functions, one which starts the timer, and one that gets the time elapsed since the start. Calling the start function again will reset the timer.

version minified size minzipped size downloads build

Install

  • npm install simple-use-timer or
  • yarn add simple-use-timer

Use

import React, { useState } from 'react';
import useTimer from 'simple-use-timer';

const TimerTest = () => {
  const [timeStarted, setTimeStarted] = useState(false);
  const [elapsedTime, setElapsedTime] = useState(0);
  const [startTime, getTime] = useTimer();

  const handleTimerClick = () => {
    if (timeStarted) {
      setElapsedTime(getTime());
    } else {
      startTime();
      setTimeStarted(true);
    }
  };

  return (
    <>
      <button type="button" onClick={handleTimerClick}>
        {timeStarted ? 'Stop Timer' : 'Start Timer'}
      </button>
      {elapsedTime > 0 && <h2>Elapsed time is {elapsedTime / 1000} seconds</h2>}
    </>
  );
};
1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago