Skip to content
Discussion options

You must be logged in to vote

Hey! I havve faced this exact scenario with tRPC + Next.js, so here’s a pattern that works well and is quite common practice:

1. Use getServerSideProps for initial load

Fetch your data via your tRPC caller and pass it down as props:

// pages/my-page.tsx
export const getServerSideProps = async (ctx) => {
  const ssg = createServerSideHelpers({ router, ctx });
  const data = await ssg.myRoute.getData.fetch();
  
  return {
    props: {
      data,
    },
  };
};

2. Hydrate tRPC’s useQuery with initial data

const { data, refetch } = trpc.myRoute.getData.useQuery(undefined, {
  initialData: props.data,
});

3. Use a mutation + optimistic update pattern

const utils = trpc.useUtils(); // v10+ ca…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by cbdseeds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants