npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

easytimer-react-hook

v2.1.0

Published

React Hook that creates a timer using EasyTimer lib

Downloads

9,803

Readme

EasyTimer React Hook

EasyTimer is a little library that allows to configure and manage a stopwatch or countdown. This React hook allows using EasyTimer with React in a very simple way.

Install

npm install --save-dev easytimer-react-hook

This hook needs React (>= v16.8) and EasyTimer.js (>= v4) in order to work:

npm install --save-dev easytimer.js react

Example

You can see a working example of this hook here: https://albert-gonzalez.github.io/easytimer-react-hook (Source Code)

Usage

useTimer hook will update the component every time that the EasyTimer instance changes its internal counter or when the target is achieved.

import useTimer from 'easytimer-react-hook';

export default () => {
    /* The hook returns an EasyTimer instance and a flag to see if the target has been achieved */
    const [timer, isTargetAchieved] = useTimer({
        /* Hook configuration */
    });

    timer.start({
        /* EasyTimer start configuration */
    });

    return <div>{timer.getTimeValues().toString()}</div>;
};

Configuration

useTimer hook accepts an object with the following options:

  • startValues: Optional. Object with the start values. The keys of these object are days, hours, minutes, seconds and secondTenths. The default value makes the timer to start from 0.
  • target: Optional. Object with the target. When the timer achieves the target, it will stop automatically. The keys of these object are days, hours, minutes, seconds and secondTenths. If no target is passed, it will be disabled.
  • precision: Optional. The frequency that the timer will update the component. The accepted precisions are hours, minutes, seconds and secondTenths. The default value is seconds.
  • countdown: Optional. If true the timer will be a countdown. The default value is false.
  • updateWhenTargetAchieved: Optional. If true the hook will update the component when the target is achieved. The default value is false.

EasyTimer instance

useTimer hook returns an EasyTimer instance. This instance is used to manage the timer (start, pause stop and reset the timer). Also, this instance can add custom event listeners if you need a specific behavior.

Check out the EasyTimer docs and examples here: https://github.com/albert-gonzalez/easytimer.js

TS Types

useTimer hook defines the following typescript types:

interface TimerHookConfig {
    startValues?: TimerValues;
    target?: TimerValues;
    precision?: Precision;
    countdown?: boolean;
    updateWhenTargetAchieved?: boolean;
}

declare type TimerHookReturn = [Timer, boolean];

declare const useTimer: ({
    startValues,
    target,
    precision,
    countdown,
    updateWhenTargetAchieved,
}?: TimerHookConfig) => TimerHookReturn;