Skip to content

feat(reactivity): untrack #13286

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

Open
wants to merge 10 commits into
base: minor
Choose a base branch
from
Open
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
23 changes: 22 additions & 1 deletion packages/reactivity/__tests__/effect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import {
serializeInner,
} from '@vue/runtime-test'
import { ITERATE_KEY, getDepFromReactive } from '../src/dep'
import { onEffectCleanup, pauseTracking, resetTracking } from '../src/effect'
import {
onEffectCleanup,
pauseTracking,
resetTracking,
untrack,
} from '../src/effect'
import {
type DebuggerEvent,
type ReactiveEffectRunner,
Expand Down Expand Up @@ -1177,6 +1182,22 @@ describe('reactivity/effect', () => {
expect(spy2).toHaveBeenCalledTimes(2)
})

it('should not track dependencies when using untrack', () => {
const a = ref(1)
const b = computed(() => a.value)
let dummyA
let dummyB
effect(() => {
dummyA = untrack(() => a.value)
dummyB = untrack(() => b.value)
})
expect(dummyA).toBe(1)
expect(dummyB).toBe(1)
a.value = 2
expect(dummyA).toBe(1)
expect(dummyB).toBe(1)
})

describe('dep unsubscribe', () => {
function getSubCount(dep: ReactiveNode | undefined) {
let count = 0
Expand Down
10 changes: 10 additions & 0 deletions packages/reactivity/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,16 @@ export function cleanup(
}
}

export function untrack<T>(fn: () => T): T {
Copy link
Member

Choose a reason for hiding this comment

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

Could we consider using MaybeRefOrGetter<T> instead of () => T here?

Copy link
Author

@teleskop150750 teleskop150750 Jul 23, 2025

Choose a reason for hiding this comment

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

Of course you can. But the utility won't be too overloaded from this. ?

Copy link
Member

Choose a reason for hiding this comment

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

I think we can just call toValue(source). In other words this will make it an untrack wrapper around toValue.

Copy link
Author

Choose a reason for hiding this comment

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

I understand you perfectly. In my mindset, passing Ref directly should be almost completely avoided. Therefore, the use of toValue is minimized. If necessary, the user can call it himself inside untrack, rather than doing it automatically each time. If you insist, I will add your edit.

Copy link
Member

Choose a reason for hiding this comment

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

It is just my opinion. I think I would prefer to hear what others think.

if (!activeSub) return fn()
const prevSub = setActiveSub()
try {
return fn()
} finally {
setActiveSub(prevSub)
}
}

/**
* Registers a cleanup function for the current active effect.
* The cleanup function is called right before the next effect run, or when the
Expand Down
1 change: 1 addition & 0 deletions packages/reactivity/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export {
enableTracking,
pauseTracking,
resetTracking,
untrack,
onEffectCleanup,
ReactiveEffect,
EffectFlags,
Expand Down
1 change: 1 addition & 0 deletions packages/runtime-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export {
toRaw,
// effect
effect,
untrack,
stop,
getCurrentWatcher,
onWatcherCleanup,
Expand Down