Skip to content
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

fix(reactivity): handle a ref directly nested in reactive #11694

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions packages/reactivity/__tests__/reactive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,14 @@ describe('reactivity/reactive', () => {
expect(isReactive(observed)).toBe(false)
})

test('a ref nested in a reactive', () => {
const obj = reactive(ref(1))
const spy1 = vi.fn(() => obj.value)
effect(spy1)
obj.value = 2
expect(isReactive(obj)).toBe(true)
})

test('hasOwnProperty edge case: Symbol values', () => {
const key = Symbol()
const obj = reactive({ [key]: 1 }) as { [key]?: 1 }
Expand Down
3 changes: 3 additions & 0 deletions packages/reactivity/src/baseHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ class BaseReactiveHandler implements ProxyHandler<Target> {
return
}

// only track its value if target is a ref
if (isRef(target) && key !== 'value') return (target as any)[key]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ignore: dep, __v_isRef, __v_isShallow, _rawValue, _value


const targetIsArray = isArray(target)

if (!isReadonly) {
Expand Down
5 changes: 4 additions & 1 deletion packages/reactivity/src/dep.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { extend, isArray, isIntegerKey, isMap, isSymbol } from '@vue/shared'
import type { ComputedRefImpl } from './computed'
import { type TrackOpTypes, TriggerOpTypes } from './constants'
import { ReactiveFlags, type TrackOpTypes, TriggerOpTypes } from './constants'
import {
type DebuggerEventExtraInfo,
EffectFlags,
Expand Down Expand Up @@ -40,6 +40,9 @@ export class Dep {
subsHead?: Link

constructor(public computed?: ComputedRefImpl | undefined) {
// @ts-expect-error
this[ReactiveFlags.SKIP] = true
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change ensures the instance of Dep non-reactive.


if (__DEV__) {
this.subsHead = undefined
}
Expand Down