Code Monkey home page Code Monkey logo

react-use-timer's Introduction

@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

Name Type Description
useTimer(time: number, options?: UseTimerOptions): UseTimerReturn function Create timer with specified time

useTimer Params

Name Type Optional Description
time number false The amount of ticks to run
options UseTimerOptions true The configuration for the timer

UseTimerReturn

Name Type Description
currentTime number The current time of the timer
start(): void function The function to start the timer
isRunning boolean If the timer is running or not
pause(): void function The function to pause the timer

UseTimerOptions

Name Type Optional Description
autoStart boolean true If the timer should start in the first render
interval number true The interval in milliseconds between ticks
onStart(): void function true The function to be called when the timer starts
onFinish(): void function true The 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

react-use-timer's People

Contributors

gabrielyotoo avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

react-use-timer's Issues

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.