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

using same query in multiple files #95

Open
Nisthar opened this issue Jun 23, 2022 · 3 comments
Open

using same query in multiple files #95

Nisthar opened this issue Jun 23, 2022 · 3 comments

Comments

@Nisthar
Copy link

Nisthar commented Jun 23, 2022

I have two components: UserHome.svelte and UserProfile.svelte.

I want to query users posts when the user navigates to UserHome.svelte. But i want the data to be stored in cache and when the user navigates to UserProfile.svelte it should have access to the cached data. How can i do this?

@Nisthar Nisthar changed the title usage in multiple files using same query in multiple files Jun 23, 2022
@sillvva
Copy link

sillvva commented Jul 6, 2022

You should be able to just use the same query key and fetcher function. If the data is still cached, it will return the results that were previously cached with that key. To avoid background re-fetches, update your cacheTime and staleTime defaults.

Caching Info: https://sveltequery.vercel.app/guides/caching
Query Options Reference: https://sveltequery.vercel.app/reference/useQuery

const queryResult = useQuery(queryKey, queryFn?, {
   cacheTime: 15 * 60 * 1000, // 15 minutes
   staleTime: 10 * 60 * 1000 // 10 minutes
 })

The difference between cache time and stale time is this:

  • Cached data is returned immediately when the component is mounted.
  • If the data is stale, it will trigger a background re-fetch and update the data.

@Nisthar
Copy link
Author

Nisthar commented Jul 6, 2022

You should be able to just use the same query key and fetcher function. If the data is still cached, it will return the results that were previously cached with that key. To avoid background re-fetches, update your cacheTime and staleTime defaults.

Caching Info: https://sveltequery.vercel.app/guides/caching Query Options Reference: https://sveltequery.vercel.app/reference/useQuery

const queryResult = useQuery(queryKey, queryFn?, {
   cacheTime: 15 * 60 * 1000, // 15 minutes
   staleTime: 10 * 60 * 1000 // 10 minutes
 })

The difference between cache time and stale time is this:

  • Cached data is returned immediately when the component is mounted.
  • If the data is stale, it will trigger a background re-fetch and update the data.

I can only seem to access it on the same page.

const getMyPostsRequest = useQuery(
		'getMyPosts',
		async () => {
			const myPosts = await api('GET', 'profile/myPosts', null, {
				currentSession: $session
			});
			return myPosts?.data;
		},
		{ staleTime: Infinity }
	);

i can access this data in the same component using useQueryClient().getQueryData('getMyPosts') but if i use it in another page, it is always undefined.
I prefer not to import the fetcher function in every components.

@multiplehats
Copy link

multiplehats commented Aug 10, 2022

Here's how I do it @Nisthar .

// $lib/queries.ts

export const useWatchlistItems = () => {
  return useQuery(
    ['watchlist', 'menuList'],
    async () => {
      const { data, error } = await getWatchlist({ orderByPrimaryKey: true });
      if (error) throw error;
      return data;
    },
    {
      refetchOnWindowFocus: false
    }
  );
};
<script lang="ts">
// some-page.svelte
import { useWatchlistItems } from '$lib/queries;

$: queryRes = useWatchlistItems();

// Do whatever..
</script>

{#if $queryRes.isLoading}
    <span>Loading...</span>
{:else if $queryRes.isError}
    <span>Error: {$queryRes.error.message}</span>
{:else if $queryRes.data}
    <div>{JSON.stringify($queryRes.data)}</div>
{/if}
<script lang="ts">
// some-componen.svelte
import { useWatchlistItems } from '$lib/queries;

$: queryRes = useWatchlistItems();

// Do whatever..
</script>

{#if $queryRes.isLoading}
    <span>Loading...</span>
{:else if $queryRes.isError}
    <span>Error: {$queryRes.error.message}</span>
{:else if $queryRes.data}
    <div>{JSON.stringify($queryRes.data)}</div>
{/if}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants