Skip to content

useLastChanged

Category
Export Size
205 B
Last Changed
5 days ago

Records the timestamp of the last change

Demo

Usage

tsx
import { 
useLastChanged
} from '@reause/shared'
import {
useState
} from 'react'
const [
a
,
setA
] =
useState
(0)
const
lastChanged
=
useLastChanged
(
a
)
// note: lastChanged is a plain number (or null), not a ref
setA
(1)
// the change is not recorded synchronously — `lastChanged` becomes the // timestamp on the render after the change, so a read right after `setA(1)` // still sees `null`

Seed the returned value before any change is recorded with initialValue (upstream: initialValue):

tsx
const 
lastChanged
=
useLastChanged
(input, {
initialValue
:
Date
.
now
() - 1000 * 60 * 5 })

Type Declarations

ts
export interface UseLastChangedOptions<InitialValue extends number | null | undefined = undefined> {
    initialValue?: InitialValue;
}

export type UseLastChangedReturn = number | null

export function useLastChanged<T>(value: T, options?: UseLastChangedOptions<undefined>): UseLastChangedReturn

export function useLastChanged<T>(value: T, options: UseLastChangedOptions<number>): number

Source

Source · Demo · VueUse

Contributors

hairyf

Changelog

v0.1.0 on
e608a - feat(ci): align CI, packaging and release with VueUse
7b7b5 - fix(shared): resolve useLastChanged audit findings (#735)

Released under the MIT License. v0.1.8