Skip to content

Commit 296ef63

Browse files
committed
Merge branch 'feat/unref_api' into feat/vue3_API
# Conflicts: # src/apis/state.ts # src/reactivity/index.ts # src/reactivity/ref.ts
2 parents e604038 + 2f3ec44 commit 296ef63

File tree

4 files changed

+13
-2
lines changed

4 files changed

+13
-2
lines changed

src/apis/state.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ export {
99
isReactive,
1010
UnwrapRef,
1111
nonReactive,
12+
unref,
1213
} from '../reactivity';

src/reactivity/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export { reactive, isReactive, nonReactive } from './reactive';
2-
export { ref, isRef, Ref, createRef, UnwrapRef, toRefs, toRef } from './ref';
2+
export { ref, isRef, Ref, createRef, UnwrapRef, toRefs, toRef, unref } from './ref';
33
export { set } from './set';

src/reactivity/ref.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ export function isRef<T>(value: any): value is Ref<T> {
111111
return value instanceof RefImpl;
112112
}
113113

114+
export function unref<T>(ref: T): T extends Ref<infer V> ? V : T {
115+
return isRef(ref) ? (ref.value as any) : ref;
116+
}
117+
114118
export function toRefs<T extends Data = Data>(obj: T): { [K in keyof T]: Ref<T[K]> } {
115119
if (!isPlainObject(obj)) return obj as any;
116120

test/apis/state.spec.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const Vue = require('vue/dist/vue.common.js');
2-
const { reactive, ref, watch, set, toRefs, computed } = require('../../src');
2+
const { reactive, ref, watch, set, toRefs, computed, unref } = require('../../src');
33

44
describe('api/ref', () => {
55
it('should work with array', () => {
@@ -331,6 +331,12 @@ describe('unwrapping', () => {
331331
expect(state.list[0].value).toBe(0);
332332
});
333333

334+
it('should unrwap ref', () => {
335+
expect(unref(0)).toBe(0);
336+
expect(unref(ref(0))).toBe(0);
337+
expect(unref({ value: 1 })).toStrictEqual({ value: 1 });
338+
});
339+
334340
it('should now unwrap plain object when using set at Array', () => {
335341
const state = reactive({
336342
list: [],

0 commit comments

Comments
 (0)