Skip to content

useWatchImmediate

Category
Export Size
196 B
Last Changed
5 days ago

Shorthand for watching value with { immediate: true }

Demo

Usage

Similar to useWatch, but the callback also fires once on mount with the current value.

tsx
import { 
useWatchImmediate
} from '@reause/shared'
import {
useState
} from 'react'
const [
obj
,
setObj
] =
useState
('vue-use')
// logs 'vue-use' on mount, then the new value on every change
useWatchImmediate
(
obj
, (
updated
) => {
console
.
log
(
updated
)
}) // later, from an event handler:
setObj
('VueUse') // logs 'VueUse'

Type Declarations

ts
export interface UseWatchCallback<T = any> {
    (value: T, oldValue: T | undefined): void;
}

export function useWatchImmediate<T extends any[]>(source: readonly [
    ...T
], callback: UseWatchCallback<[
    ...T
]>): void

export function useWatchImmediate<T>(source: T, callback: UseWatchCallback<T>): void

export function useWatch<T extends any[]>(source: readonly [
    ...T
], callback: UseWatchCallback<[
    ...T
]>, options?: UseWatchOptions): void

export function useWatch<T>(source: T, callback: UseWatchCallback<T>, options?: UseWatchOptions): void

Source

Source · Demo · VueUse

Contributors

hairyf

Changelog

v0.1.0 on
59695 - fix(shared): resolve useWatchImmediate audit findings (#767)

Released under the MIT License. v0.1.8