delay-function-hook
v1.1.0
Published
React hook component make delay your function.
Downloads
3
Readme
Delay Function Hook
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>
);
}
Params
The hook returns a function. The function return a promise and accepts arguments:
callback
: Function (required);milliseconds
: number (required);- Your list arguments?: any;