Skip to content

useStateAutoReset

Category
Export Size
717 B
Last Changed
5 days ago

A controllable state which will be reset to the default value after some time.

Demo

Usage

tsx
import { 
useStateAutoReset
} from '@reause/shared'
const [
message
,
setMessage
] =
useStateAutoReset
('default message', 1000)
function
handleMessage
() {
// here the value will change to 'message has set' but after 1000ms, it will change to 'default message'
setMessage
('message has set')
}

NOTE

You can reassign the entire object to trigger updates after making deep mutations to the inner value.

Type Declarations

ts
export type UseStateAutoResetReturn<T = any> = [
    T,
    Dispatch<SetStateAction<T>>
]

export type State<T> = StateValue<T>

export function useStateAutoReset<T = any>(defaultValue: State<T>, afterMs?: number): UseStateAutoResetReturn<T>

export function useControllableState<T>(state: State<T>, options?: UseControllableStateOptions<T>): StateTuple<T>

Source

Source · Demo · VueUse

Contributors

hairyf

Changelog

v0.1.0 on
0e589 - fix(shared): resolve useStateAutoReset audit findings (#742)
464ef - feat(shared): add useStateAutoReset (#49)

Released under the MIT License. v0.1.8