-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
[svelte] "placeholderData: keepPreviousData" doesn't work on reactive queries #5913
Comments
@lachlancollins FYI |
could you try this $: query = createQuery({ setting the query key to be post will cause the the query to be re-evaluated everytime the post changes and cause the query to "restart" instead of picking up from where it left off..so just remove the reactive values like the post id from the query key and just place the reactive values on the url. |
Having no key would mean there would be no cache.
|
@HugeLetters i'm not sure if i have same issue has you keepPreviusData is getting last "queryFn" executed instead the last queryKey are used, I'n my case i have executed url with params in querykey: fetching post 1 -> displays undefined fetching post 2 -> displays post 1 from prev data .. return to post 1 fetch post 3 -> DISPLAY POST 2 I updated to latest version to V5 |
Hi guys. I think the issue you're having is explained in #5682. Use stores instead of reactive query declaration and let me know. See new svelte doc for V5 too. |
@frederikhors Using a store works but not everything can be a store. When I bind a local state to some input and base my query on that - using a My point being that I think svelte-query should work with reactive statements too, it's pretty standard in Svelte - the fact that it recreates the whole Please don't take my comment as "fix this immediately", I'm just giving my thoughts on how I think this should work and why I think that is. |
I know it's a hack and it might not work for v5 but if you're already using custom methods to create your queries, you can keep previous data with currying. Here is a minimal (pseudo-code) example, just make sure your version of function createReactiveQuery (key, query) {
// Keep track of previous data
let previousData
return (input) => {
const queryStore = createQuery({
queryKey: [key, input],
queryFn: (context) => query(context.queryKey[1]),
// Return previous data as placeholder data
placeholderData: () => previousData,
})
// Keep previous data updated
queryStore.subscribe((v) => (previousData = v.data))
return queryStore
}
} You can now create reactive queries like so: export let data
const myReactiveQuery = createReactiveQuery('myQuery', myQueryFn)
$: dataPoints = myReactiveQuery({ myData: data }) |
@MoritzKronberger yup, did something very similar let id: string;
let prevData: Chat;
$: chatQuery = createQuery({
queryFn() {
return getChat(id);
},
queryKey: ["chat", id],
placeholderData: prevData,
});
// ?. because $chatQuery is undefined on initial render actually
$: if ($chatQuery?.isSuccess) {
prevData = $charQuery.data;
} |
@HugeLetters Yes, I think your version shows the underlying "problem" of |
@MoritzKronberger but react also does reassignments essentially on each rerender and it doesn't seem to cause any issues. |
this issue looks a bit stale. please let me know how to proceed here. Otherwise I'll close it as a known limitation because the workaround seems to be "Using a store". @lachlancollins fyi |
Given that this issue might be solved completely with Svelte 5 runes I don't think this issue is severe enough to warrant a fix given there's a workaround |
@HugeLetters
|
Describe the bug
placeholderData: keepPreviousData
doesn't work correctly with reactive queries. It still showsundefined
when changing keys.Your minimal, reproducible example
https://stackblitz.com/edit/sveltejs-kit-template-default-fcwhut?file=src%2Froutes%2F%2Bpage.svelte
Steps to reproduce
Expected behavior
I expect to see a previous post until a new post is successfully fetched.
How often does this bug happen?
Every time
Screenshots or Videos
2023-08-26.13-42-19.mp4
Platform
Tanstack Query adapter
svelte-query
TanStack Query version
5.0.0-beta.20
TypeScript version
5.2.2
Additional context
Perhaps I'm just using this wrong - I assume query gets recreated on each post update, and also the keys update and that's why svelte-query cant' really understand where to get the previous data from?
The text was updated successfully, but these errors were encountered: