Fix $watch firing callback when primitive value unchanged#4732
Merged
calebporzio merged 1 commit intomainfrom Feb 1, 2026
Merged
Fix $watch firing callback when primitive value unchanged#4732calebporzio merged 1 commit intomainfrom
calebporzio merged 1 commit intomainfrom
Conversation
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When using
$watchon a deeply nested primitive property (e.g.,$watch('foo.bar.baz', callback)), the callback fires even when the actual value hasn't changed. This happens when you replace a parent object with a new one that contains the same nested value:Investigation
Tested Vue 3's actual runtime behavior to establish the expected behavior:
Vue compares primitives by value and objects by reference.
Alpine's
watch()function inreactivity.jswas calling the callback unconditionally whenever the effect re-ran, without comparing old and new values.Solution
Added value comparison before firing the callback:
!==, skip callback if unchangedJSON.stringifymay have detected nested mutations, and object reference comparison would correctly fire for new objects anyway)Also moved
oldValueassignment outside the conditional to ensure it's always updated synchronously, preventing stale comparisons on subsequent effect runs.