Description
Let's say I'm using createServerQuery
to prefetch a list of users on the server and pass the query to the client:
// +page.ts
import type { PageLoad } from './$types';
import { trpc } from '$lib/trpc/client';
export const load: PageLoad = async (event) => {
const { queryClient } = await event.parent();
const client = trpc(event, queryClient);
return {
users: await client.user.all.createServerQuery(),
};
};
I can use the query result to display content accordingly:
<!-- +page.svelte -->
<script lang="ts">
import type { PageData } from './$types';
export let data: PageData;
const users = data.users();
</script>
<h1>List of users</h1>
{#if $users.isFetching}
<p>Loading...</p>
{:else if $users.error}
<p>Error: {$users.error.message}</p>
{:else if $users.isSuccess}
<ul>
{#each $users.data as user}
<li>{user.name}</li>
{/each}
</ul>
{:else}
<p>No users found</p>
{/if}
Now I want to filter the list of users as someone types in an input field. This requires me to send an input to the query. My guess was I would be able to pass the input to data.users()
just like I can pass the input to trpc($page).user.all.createQuery(input)
but this does not work and I could not find an example to achieve this.
<!-- +page.svelte -->
<script lang="ts">
import type { PageData } from './$types';
export let data: PageData;
let filter = '';
$: users = data.users({ name: filter });
</script>
<input type="text" bind:value={filter} placeholder="Filter users" />
...
I might be missing something really obvious here as this seems to me like a very common use case: fetching data on the server, passing it to the client so that the client doesn't have to load it again when it hydrates and then the subsequent loading of data happens on the client when inputs vary depending on user actions.
Apologies if this has been already answered somewhere, I dug into the README and issues but couldn't find anything.