whenever
watching的值为truth的简便写法
用法
js
import { whenever } from '@mpxjs/mpxuse-core'
whenever(isReady, () => console.log(state))
import { whenever } from '@mpxjs/mpxuse-core'
whenever(isReady, () => console.log(state))
ts
// this
whenever(ready, () => console.log(state))
// is equivalent to:
watch(ready, (isReady) => {
if (isReady)
console.log(state)
})
// this
whenever(ready, () => console.log(state))
// is equivalent to:
watch(ready, (isReady) => {
if (isReady)
console.log(state)
})
Callback Function
与watch
相同,回调将使用cb(value, oldValue, onInvalidate)
调用。
ts
whenever(height, (current, lastHeight) => {
if (current > lastHeight)
console.log(`Increasing height by ${current - lastHeight}`)
})
whenever(height, (current, lastHeight) => {
if (current > lastHeight)
console.log(`Increasing height by ${current - lastHeight}`)
})
Computed
与watch
相同,您可以传递getter函数来计算每个更改。
ts
// this
whenever(
() => counter.value === 7,
() => console.log('counter is 7 now!'),
)
// this
whenever(
() => counter.value === 7,
() => console.log('counter is 7 now!'),
)
Options
选项和默认值与watch
相同。
ts
// this
whenever(
() => counter.value === 7,
() => console.log('counter is 7 now!'),
{ flush: 'sync' },
)
// this
whenever(
() => counter.value === 7,
() => console.log('counter is 7 now!'),
{ flush: 'sync' },
)
类型声明
typescript
/**
* Shorthand for watching value to be truthy
*
* @see https://mpxuse.cn/whenever
*/
export declare function whenever<T>(
source: WatchSource<T | false | null | undefined>,
cb: WatchCallback<T>,
options?: WatchOptions
): () => void
/**
* Shorthand for watching value to be truthy
*
* @see https://mpxuse.cn/whenever
*/
export declare function whenever<T>(
source: WatchSource<T | false | null | undefined>,
cb: WatchCallback<T>,
options?: WatchOptions
): () => void