An async-utility hook that accepts a callback function
and a delay time
(in milliseconds), then repeat the
execution of the given function by the defined time.
- takes care of performing the given callback regardless the component re-renders;
- cancels the interval when the component unmount (or not, depends by the defined options);
- returns the interval state (cleared/not cleared)
- returns a method to possibly cancel the set interval (cause the component re-render)
import { useState } from 'react';
import { useInterval } from 'beautiful-react-hooks';
const DelayedContentComponent = () => {
const [seconds, setSeconds] = useState(0);
// repeat the function each 1000ms
useInterval(() => {
setSeconds(1 + seconds);
}, 1000);
return (
<DisplayDemo>
<p>Rendering since {seconds} seconds</p>
</DisplayDemo>
);
};
<DelayedContentComponent />
The hook returns the state of the timeout (a boolean, cleared/not cleared) and a method to possibly clear it.
Note: programmatically clearing the timeout will cause the component re-render.
import { useState } from 'react';
import { useInterval } from 'beautiful-react-hooks';
const DelayedContentComponent = () => {
const [seconds, setSeconds] = useState(0);
const [isCleared, clearInterval] = useInterval(() => {
setSeconds(1 + seconds);
}, 1000);
return (
<DisplayDemo>
<p>Rendering since {seconds} seconds</p>
{!isCleared && <button onClick={clearInterval}>Stop it!</button>}
{isCleared && <p>Interval cleared!</p>}
</DisplayDemo>
);
};
<DelayedContentComponent />
useInterval
might accept a options object provided as eventual parameter.
Defines whether the timeout should be cleared on unmount.
default: true
import { useState } from 'react';
import { useInterval } from 'beautiful-react-hooks';
const DelayedContentComponent = () => {
const [seconds, setSeconds] = useState(0);
const options = { cancelOnUnmount: false };
useInterval(() => setSeconds(1 + seconds), 1000, options);
return (
<DisplayDemo>
<p>Content rendering since {seconds} but will not be cleared on unmount</p>
</DisplayDemo>
);
};
<DelayedContentComponent />
- If in need to perform a function every x number of milliseconds regardless the component re-renders
- You can't use it asynchronously since this will break the rules of hooks