Module: use-resettable-state
Table of contents
Functions
Functions
useResettableState
▸ useResettableState<T
>(initialState
, delay
): any
[]
A custom React hook that provides a state variable along with a function to temporarily set its value. After a specified delay, the state automatically resets to its initial value.
Type parameters
Name | Description |
---|---|
T | The type of the state variable. |
Parameters
Name | Type | Description |
---|---|---|
initialState | any | The initial value of the state. This is also the value to which the state will reset. |
delay | any | The duration in milliseconds after which the state will reset to the initialState. |
Returns
any
[]
A tuple containing the current state and a function to set the state temporarily.
The function accepts a new value and after delay
milliseconds, it resets the state to initialState
.
Example
const [count, setCountTemp] = useResettableState(0, 1000);
setCountTemp(5); // Sets count to 5, then resets to 0 after 1000 milliseconds
Defined in
react/dist/esm/use-resettable-state/index.js:18