Skip to content

fix(reactivity): ref as a reactive parameter should return (#6358) #6359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(reactivity): ref as a reactive parameter should return (#6358)
  • Loading branch information
hubvue committed Jul 26, 2022
commit 6b85ada7dd236d6219937fd5b47dbee420a21d3f
10 changes: 10 additions & 0 deletions packages/reactivity/__tests__/ref.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,16 @@ describe('reactivity/ref', () => {
expect(spy2).toBeCalledTimes(1)
})

test('ref is called as an argument to reactive, effectFn should only be triggered once', () => {
const obj = reactive(ref(1))
const spy1 = jest.fn(() => obj.value)

effect(spy1)

obj.value = 2
expect(spy1).toBeCalledTimes(2)
})

test('ref should preserve value shallow/readonly-ness', () => {
const original = {}
const r = reactive(original)
Expand Down
4 changes: 2 additions & 2 deletions packages/reactivity/src/reactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
shallowCollectionHandlers,
shallowReadonlyCollectionHandlers
} from './collectionHandlers'
import type { UnwrapRefSimple, Ref, RawSymbol } from './ref'
import { UnwrapRefSimple, Ref, RawSymbol, isRef } from './ref'

export const enum ReactiveFlags {
SKIP = '__v_skip',
Expand Down Expand Up @@ -89,7 +89,7 @@ export type UnwrapNestedRefs<T> = T extends Ref ? T : UnwrapRefSimple<T>
export function reactive<T extends object>(target: T): UnwrapNestedRefs<T>
export function reactive(target: object) {
// if trying to observe a readonly proxy, return the readonly version.
if (isReadonly(target)) {
if (isReadonly(target) || isRef(target)) {
return target
}
return createReactiveObject(
Expand Down