1.1.0 • Published 3 years ago

delay-function-hook v1.1.0

Weekly downloads
-
License
CC0-1.0
Repository
github
Last release
3 years ago

Delay Function Hook

npm version

A React Hook component delays the execution of your function. If your function is called a second time then the first call will be canceled. The function return promise and you can use construction then/catch.

  • a small function
  • implemented in typescript
  • no dependencies

Installation

    #npm
    $ npm install delay-function-hook
    
    #yarn
    $ yarn add delay-function-hook

Usage

import React from 'react';
import { useDelayFunction } from 'delay-function-hook/lib';

export default function App() {
  const delay = useDelayFunction();

  const searchDataByQueryText = queryText => {
    console.log('Run search data by: ' + queryText);

    return 'complete';
  }

  const handleKeyUp = (event) => {
    const userText = event.target.value;

    delay(searchDataByQueryText, 1500, userText)
      .then(result => console.log('Result: ' + result))
      .catch(error => console.error('Error: ' + error));
  };

  return (
    <div className="App">
      <textarea onKeyUp={handleKeyUp} />
    </div>
  );
}

npm.io

Params

The hook returns a function. The function return a promise and accepts arguments:

  • callback: Function (required);
  • milliseconds: number (required);
  • Your list arguments?: any;
1.1.0

3 years ago

1.0.0

3 years ago

0.1.0

3 years ago