2.3.0 • Published 1 year ago

@gabrielyotoo/react-use-timer v2.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

@gabrielyotoo/react-use-timer · GitHub licensenpm version

react-use-timer is a hook to help you with timers in React.

Example

Installation

To install run the following command in your project's folder if you are using yarn

yarn add @gabrielyotoo/react-use-timer

or if you are using npm

npm install @gabrielyotoo/react-use-timer

Usage

A simple timer example showing the current time from 10 to 0.

import { useTimer } from '@gabrielyotoo/react-use-timer';

function App() {
  const [finished, setFinished] = useState(false);

  const { currentTime, isRunning, start, pause } = useTimer(10, {
    onFinish: () => {
      setFinished(true);
    },
    onStart: () => {
      setFinished(false);
    },
  });

  return (
    <>
      {finished ? <p>Finished!</p> : null}
      <div>
        {!isRunning ? (
          <p>Not running! Timer at {currentTime}</p>
        ) : (
          <p>Timer at {currentTime}</p>
        )}
        <button onClick={start}>{!isRunning ? 'Start!' : 'Reset'}</button>
        {isRunning ? <button onClick={pause}>Pause</button> : null}
      </div>
    </>
  );
}

You can also run the example project.

API

NameTypeDescription
useTimer(time: number, options?: UseTimerOptions): UseTimerReturnfunctionCreate timer with specified time

useTimer Params

NameTypeOptionalDescription
timenumberfalseThe amount of ticks to run
optionsUseTimerOptionstrueThe configuration for the timer

UseTimerReturn

NameTypeDescription
currentTimenumberThe current time of the timer
start(): voidfunctionThe function to start the timer
isRunningbooleanIf the timer is running or not
pause(): voidfunctionThe function to pause the timer

UseTimerOptions

NameTypeOptionalDescription
autoStartbooleantrueIf the timer should start in the first render
intervalnumbertrueThe interval in milliseconds between ticks
onStart(): voidfunctiontrueThe function to be called when the timer starts
onFinish(): voidfunctiontrueThe function to be called after the time's up

Contributing

Please, feel free to create issues and to open PRs. Be cautious to the tests and builds, because if your version doesn't pass they, your PR won't be approved and merged

2.3.0

1 year ago

2.2.0

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.2.0

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.1.3

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago