Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/whenever.md
1---2category: Watch3---45# whenever67Shorthand for watching value to be truthy.89## Usage1011```js12import { useAsyncState, whenever } from '@vueuse/core'1314const { state, isReady } = useAsyncState(15fetch('https://jsonplaceholder.typicode.com/todos/1').then(t => t.json()),16{},17)1819whenever(isReady, () => console.log(state))20```2122```ts23import { whenever } from '@vueuse/core'24// ---cut---25// this26whenever(ready, () => console.log(state))2728// is equivalent to:29watch(ready, (isReady) => {30if (isReady)31console.log(state)32})33```3435### Callback Function3637Same as `watch`, the callback will be called with `cb(value, oldValue, onInvalidate)`.3839```ts40import { whenever } from '@vueuse/core'41// ---cut---42whenever(height, (current, lastHeight) => {43if (current > lastHeight)44console.log(`Increasing height by ${current - lastHeight}`)45})46```4748### Computed4950Same as `watch`, you can pass a getter function to calculate on each change.5152```ts53import { whenever } from '@vueuse/core'54// ---cut---55// this56whenever(57() => counter.value === 7,58() => console.log('counter is 7 now!'),59)60```6162### Options6364Options and defaults are same with `watch`.6566```ts67import { whenever } from '@vueuse/core'68// ---cut---69// this70whenever(71() => counter.value === 7,72() => console.log('counter is 7 now!'),73{ flush: 'sync' },74)75```7677## Type Declarations7879```ts80export interface WheneverOptions extends WatchOptions {81/**82* Only trigger once when the condition is met83*84* Override the `once` option in `WatchOptions`85*86* @default false87*/88once?: boolean89}90/**91* Shorthand for watching value to be truthy92*93* @see https://vueuse.org/whenever94*/95export declare function whenever<T>(96source: WatchSource<T | false | null | undefined>,97cb: WatchCallback<T>,98options?: WheneverOptions,99): WatchHandle100```101