use-timeout-mock-interval
v1.0.0
Published
A custom React Hook that provides a declarative setTimeout mock setInterval called useTimeoutMockInterval.
Downloads
39
Readme
use-timeout-mock-interval
A custom React Hook that provides a declarative setTimeout mock setInterval called useTimeoutMockInterval.
Installation
$ npm i use-timeout-mock-interval
or
$ yarn add use-timeout-mock-interval
Usage
useTimeoutMockInterval(fn, delay);
Parameters
Here are the parameters that you can use.
| Parameter | Description |
| :--------- | :------------------------------------------------------------------------------- |
| fn
| A function that will be called every delay
milliseconds. |
| delay
| A number representing the delay in msecs. Set to null
to "pause" the interval. |
Example
import React, { useState } from 'react';
import useTimeoutMockInterval from 'use-timeout-mock-interval';
const Counter = ({ delay = 1000 }) => {
const [count, setCount] = useState(0);
useTimeoutMockInterval(() => {
setCount((currentCount) => currentCount + 1);
}, delay);
return <h1>{count}</h1>;
};
export default Counter;
License
MIT Licensed