Appearance
useWatchArray
Watches an array value and reports which items were added and removed since the previous list
Demo
Usage
tsx
import { useWatchArray } from '@reause/shared'
useWatchArray(list, (newList, oldList, added, removed) => {
console.log(`added: ${added}`, `removed: ${removed}`)
})
// fire once on mount with the current list
useWatchArray(list, (newList, oldList, added, removed) => {
console.log(newList, oldList, added, removed)
}, { immediate: true })Type Declarations
ts
export interface UseWatchArrayCallback<V = any, OV = any> {
(value: V, oldValue: OV, added: V, removed: OV): void;
}
export interface UseWatchArrayOptions<Immediate extends Readonly<boolean> = false> {
immediate?: Immediate;
}
export function useWatchArray<T, Immediate extends Readonly<boolean> = false>(source: T[], cb: UseWatchArrayCallback<T[], Immediate extends true ? T[] | undefined : T[]>, options?: UseWatchArrayOptions<Immediate>): void