-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(reactiveComputed): new function
- Loading branch information
Showing
3 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
category: Utilities | ||
--- | ||
|
||
# reactiveComputed | ||
|
||
Computed reactive object. Instead of returning a ref that `computed` does, `reactiveComputed` returns a reactive object. | ||
|
||
## Usage | ||
|
||
```ts | ||
import { reactiveComputed } from '@vueuse/core' | ||
|
||
const state = reactiveComputed(() => { | ||
return { | ||
foo: 'bar', | ||
bar: 'baz' | ||
} | ||
}) | ||
|
||
state.bar // 'baz' | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { nextTick, ref, watch, watchEffect } from 'vue-demi' | ||
import { reactiveComputed } from '.' | ||
|
||
describe('reactiveComputed', () => { | ||
it('should work', () => { | ||
const count = ref(0) | ||
|
||
const state = reactiveComputed(() => { | ||
return { | ||
count: count.value, | ||
} | ||
}) | ||
|
||
expect(state.count).toBe(0) | ||
|
||
count.value++ | ||
|
||
expect(state.count).toBe(1) | ||
}) | ||
|
||
it('should work with dynamic props', async() => { | ||
const foo = ref(false) | ||
|
||
const state = reactiveComputed(() => { | ||
return foo.value | ||
? { foo: true, type: 'foo' } | ||
: { bar: true, type: 'bar' } | ||
}) | ||
|
||
let dummy = 0 | ||
let type | ||
|
||
watch(state, () => { | ||
dummy += 1 | ||
}) | ||
watchEffect(() => { | ||
type = state.type | ||
}) | ||
|
||
expect(state.foo).toBe(undefined) | ||
expect(state.bar).toBe(true) | ||
expect(type).toBe('bar') | ||
|
||
foo.value = true | ||
|
||
expect(state.foo).toBe(true) | ||
expect(state.bar).toBe(undefined) | ||
|
||
await nextTick() | ||
|
||
expect(dummy).toBe(1) | ||
expect(type).toBe('foo') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { computed } from 'vue-demi' | ||
import { toReactive } from '../toReactive' | ||
|
||
/** | ||
* Computed reactive object. | ||
*/ | ||
export function reactiveComputed<T extends {}>(fn: () => T): Readonly<T> { | ||
return toReactive(computed(() => fn())) | ||
} |