fix: make queryKey reactive for dynamic input changes (#fix-reactive-…#239
Open
zxzinn wants to merge 1 commit intowobsoriano:mainfrom
Open
fix: make queryKey reactive for dynamic input changes (#fix-reactive-…#239zxzinn wants to merge 1 commit intowobsoriano:mainfrom
zxzinn wants to merge 1 commit intowobsoriano:mainfrom
Conversation
…query-key) Make queryKey a getter so it updates when input changes. This ensures useAsyncData uses the correct cache key for reactive inputs.
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.
Fix: Make queryKey reactive for dynamic input changes
Problem
When using
useQuerywith a reactiveinput(e.g., acomputed), thequeryKeyis computed once at call time and never updates. This causesuseAsyncDatato use a stale cache key, returning incorrect cached data when the input changes.Reproduction
Expected: When
selectedIdchanges from1to2, the query should return data for item2.Actual: The query fetches item
2from the server, butuseAsyncDatareturns cached data for item1because thequeryKeyis still based on the initial input.Root Cause
In
decoration-proxy.tsline 33:queryKeyis computed once whenhandleUseQueryis called. Even thoughinputis reactive,queryKeybecomes a static string value that never updates.Solution
Make
queryKeya getter function so it re-evaluates wheninputchanges:This ensures:
inputis reactive,queryKeyreturns a new cache key based on the current input valuecustomQueryKeyis provided as a ref/getter, it's passed through as-iscustomQueryKeyis a static value, it's wrapped in a getter for consistencyTesting
useQueryusing acomputedinput