@rooks/use-timeout

A React Hooks package for timeout

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
@rooks/use-timeout
2,81644.11.22 years ago4 years agoMinified + gzip package size for @rooks/use-timeout in KB

Readme

@rooks/use-timeout

Note: Future updates to this package have moved to the main package rooks. All hooks now reside in a single package which you can install using

npm install rooks

or
yarn add rooks

Rooks is completely treeshakeable and if you use only 1 of the 50+ hooks in the package, only that hook will be bundled with your code. Your bundle will only contain the hooks that you need. Cheers!
TitleCard
Build Status

About

setTimeout hook for react.

Installation

npm install --save @rooks/use-timeout

Importing the hook

import useTimeout from "@rooks/use-timeout"

Usage

function TimeoutComponent() {
  function doAlert() {
    window.alert("timeout expired!");
  }
  const { start, clear } = useTimeout(doAlert, 2000);
  return (
    <>
      <button onClick={start}> Start timeout </button>
      <button onClick={clear}> Clear timeout </button>
    </>
  );
}

render(<TimeoutComponent/>)

Arguments

| Arguments | Type | Description | Default value | | --------- | -------- | -------------------------------------------------------- | ------------- | | callback | function | Function to be executed in timeout | undefind | | delay | Number | Number in milliseconds after which callback is to be run | 0 |

Returned Object keys

| Returned object attributes | Type | Description | | -------------------------- | -------- | --------------------- | | clear | function | Clear the timeout | | start | function | Start the timeout | | isActive | boolean | Is the timeout active |