Category | Watch |
---|---|
Export Size | 97 B |
Last Changed | 9 months ago |
Shorthand for watching value to be truthy.
import { useAsyncState, whenever } from '@vueuse/core' const { state, isReady } = useAsyncState( fetch('https://jsonplaceholder.typicode.com/todos/1').then(t => t.json()), {}, ) whenever(isReady, () => console.log(state))
// this whenever(ready, () => console.log(state)) // is equivalent to: watch(ready, (isReady) => { if (isReady) console.log(state) })
Same as watch
, the callback will be called with cb(value, oldValue, onInvalidate)
.
whenever(height, (current, lastHeight) => { if (current > lastHeight) console.log(`Increasing height by ${current - lastHeight}`) })
Same as watch
, you can pass a getter function to calculate on each change.
// this whenever( () => counter.value === 7, () => console.log('counter is 7 now!'), )
Options and defaults are same with watch
.
// this whenever( () => counter.value === 7, () => console.log('counter is 7 now!'), { flush: 'sync' }, )
/** * Shorthand for watching value to be truthy * * @see https://vueuse.org/whenever */ export declare function whenever<T>( source: WatchSource<T | false | null | undefined>, cb: WatchCallback<T>, options?: WatchOptions ): WatchStopHandle
© 2019-present Anthony Fu
Licensed under the MIT License.
https://vueuse.org/shared/whenever/