# useConditionalTimeout
An asynchronous hook which takes in three parameters: a "callback", a "delay time" (in milliseconds), and a boolean value known as "
condition".\
It then postpones the execution of the given callback by the specified delay time, only when the provided condition changes to `true`
### 💡 Why?
- To start a timeout only after a certain condition has been confirmed;
- Handles the executing of the provided callback despite the component's re-rendering;
- Terminates the timeout when the component unmounts (or not, depending on the specified options) and/or when the provided condition is
confirmed;
### Basic Usage:
```jsx harmony
import { useState } from 'react';
import { Button, Space, Typography } from 'antd';
import useConditionalTimeout from 'beautiful-react-hooks/useConditionalTimeout';
const ConditionalDelayedContentComponent = () => {
const [condition, setCondition] = useState(false);
const [showContent, setShowContent] = useState(false);
useConditionalTimeout(() => {
setShowContent(true)
}, 2000, condition);
const Actions = [
]
return (
Click on the following button to change the condition that triggers the 2 seconds timeout to true
After timeout is elapsed a content is displayed
{showContent &&
🕰
}
)
};
```
### State & clear method:
The hook will return the state of the timeout (either cleared or not cleared) and a function that may be used to clear it.
```jsx harmony
import { useState } from 'react';
import { Button, Typography } from 'antd';
import useConditionalTimeout from 'beautiful-react-hooks/useConditionalTimeout';
const ConditionalDelayedContentComponent = () => {
const [condition, setCondition] = useState(false);
const [showContent, setShowContent] = useState(false);
const [isCleared, clearTimeoutRef] = useConditionalTimeout(() => {
setShowContent(true)
}, 5000, condition);
const Actions = [
]
return (
Content will show after 5 second starting from the following button click
{showContent &&
🕰
}
{!isCleared && !showContent && }
{isCleared && Cleared}
)
};
```
### Options:
The third parameter is an optional object of options
#### cancelOnUnmount:
Defines whether the timeout should be cleared on unmount.
**default**: `true`
```jsx harmony
import { useState } from 'react';
import { Button } from 'antd';
import useConditionalTimeout from 'beautiful-react-hooks/useConditionalTimeout';
const ConditionalDelayedContentComponent = () => {
const [condition, setCondition] = useState(false);
const [showContent, setShowContent] = useState(false);
const options = { cancelOnUnmount: false };
useConditionalTimeout(() => {
setShowContent(true)
}, 5000, condition, options);
return (
{showContent &&
🕰
}
)
};
```
#### cancelOnConditionChange:
Defines whether the timeout should be cleared when the condition changes.
In this example, clicking on the button will not trigger any action as there are two instances of useConditionalTimeout, and one of them
will modify the condition.
**default**: `true`
```jsx harmony
import { useState } from 'react';
import { Button } from 'antd';
import useConditionalTimeout from 'beautiful-react-hooks/useConditionalTimeout';
const ConditionalDelayedContentComponent = () => {
const [condition, setCondition] = useState(false);
const [showContent, setShowContent] = useState(false);
useConditionalTimeout(() => {
setShowContent(true)
}, 5000, condition);
useConditionalTimeout(() => {
setCondition(false)
}, 2000, condition);
return (
{showContent &&
🕰
}
)
};
```
### Mastering the hooks
#### ✅ When to use
- If it is necessary to execute a callback after a specific duration and only when a specific condition has been verified.
### Types
```typescript static
import { type GenericFunction } from './shared/types';
/**
* An async-utility hook that accepts a callback function and a delay time (in milliseconds), then delays the
* execution of the given function by the defined time from when the condition verifies.
*/
declare const useConditionalTimeout: (fn: TCallback, milliseconds: number, condition: boolean, options?: UseConditionalTimeoutOptios) => UseConditionalTimeoutReturn;
export interface UseConditionalTimeoutOptios {
cancelOnUnmount?: boolean;
cancelOnConditionChange?: boolean;
}
export type UseConditionalTimeoutReturn = [boolean, () => void];
export default useConditionalTimeout;
```