This allows you to use the well known setTimeout method inside a React Functional Component. Very similar to the useInterval
hook. When the delay
reached, the callback is executed. Could be great to hide or display something with delay.
const useTimeout = (callback: () => void, delay: number | null): void => {
// ....
};
import React, { useState } from 'react';
import { useTimeout } from '@knightburton/react-hooks-toolkit';
const App = () => {
const [visible, setVisible] = useState<boolean>(false);
useTimeout(() => setVisible(false), 1000);
return (
<p>
The following message is important, you must remember to it!
<strong>{visible ? 'The answer is 42.' : 'I hope you have got it ;)'}</strong>
</p>
);
};
export default App;