Next.js Discord

Discord Forum

prerender react query data

Answered
Sloth bear posted this in #help-forum
Open in Discord
Sloth bearOP
'use client'
import React from 'react'
import { useQuery } from '@tanstack/react-query'
import axios from 'axios';

const question = () => {

  const { data, isLoading } = useQuery({
    queryKey: ['QUESTION'],
    queryFn: async () => {
      const response = await axios.get('http://localhost:8081/api/question/getQuestionTest');
      return response.data.question;
    }
  })


  if(isLoading) {
    return (
      <main className="flex min-h-screen flex-col items-center justify-between p-24">
        <p>
          Loading...
        </p>
      </main>
    )
  }

  return (
    <main className="flex min-h-screen flex-col items-center justify-between p-24">
      <p>
        {
          data && (
            <div>
              <h2>{data.question}</h2>
            </div>
          )
        }
      </p>
    </main>
  )
}

export default question


import { dehydrate, HydrationBoundary } from '@tanstack/react-query';
import getQueryClient from '@/lib/getQueryClient';
import Question from '@/components/question/index';

export default async function index() {

  const queryClient = getQueryClient()
  await queryClient.prefetchQuery({
    querykey: ['QUESTION'],
    queryFn: async () => {
      const response = await axios.get('http://localhost:8081/api/question/getQuestionTest');
      return response.data.question;
    }
  })
  const dehydratedState = dehydrate(queryClient)

  return (
    <main>
      <h1 className='mt-10 p-10 w-full m-auto text-center border rounded-full' >
        Question Page
      </h1>
      <HydrationBoundary state={dehydratedState}>
        <Question />
      </HydrationBoundary>
    </main>
  )
}


this is working fine but i was expecting data to be prerendered on server and still be dynamic or just behave normally how its on client side.
any pointers what i am missing here or i'm maybe approaching this wrongly ?
Answered by European sprat
but fix that typo and i think your problem is solved
View full answer

52 Replies

European sprat
"this is working fine but i was expecting data to be prerendered on server and still be dynamic or just behave normally how its on client side."

can you describe what issue you're seeing then?
Sloth bearOP
thnks for the response,
correct me if i am wrong, this query fetches data on client side which is not what i want but i want this query data to be pre rendered on server due to SEO concerns. we can simply just do that by fetch but with with react-query i want that data to be prerendered on server and when its on client it refetches or invalidates the query like how it normally acts on client side.
European sprat
yeah but what you have shown seems like it's setup to do that
Sloth bearOP
but why's there a request for questions in network tab, and also theres nothing in the query data initially (when page is first loaded)
European sprat
it'll show a request because it will fetch it immediately on the client side too
but if you look in react query tools you should also be able to see the "initialState" being populated. is that empty?
Sloth bearOP
i've loged the data from query and it seemed empty
ofc on client component that is
European sprat
use the react query devtools and look at the query explorer for the query name and check if there's an initalState or not
the code you have shared looks almost the same as what i'm using and mine is working
if you can make a minimal reproduction repo i can check it later today
Sloth bearOP
oh
and i am using v5
of react-query
European sprat
i don't know then, it's beta
make the min repro and i'll check it
Sloth bearOP
ya let me switch it back to stable
k will do
gimme a sec
Sloth bearOP
European sprat
"querykey" typo
Sloth bearOP
please clear one confusion for me, the query data is rendered on server ? or it it passed to client component as inital state (and we know client component run when they arrive on client side).
European sprat
client components are also pre rendered on the server
Sloth bearOP
so the ones marked 'use client' are also rendered on server ? then how do they utilize hooks ? and other windows api
@Sloth bear so the ones marked 'use client' are also rendered on server ? then how do they utilize hooks ? and other windows api
European sprat
those things aren't evaluated by the server. sometimes you do need to check for window being defined depending on what you are using
European sprat
but fix that typo and i think your problem is solved
Answer
European sprat
i saw there were console errors about the naming of the keys which had me look to see it was typed wrong
Sloth bearOP
the key is typed wrong ?
QUESTION both are same though
European sprat
it needs to be queryKey
not the capital K
Sloth bearOP
omg
it was
that was so dumb of me
thnk u soooo much man, i've been bangin my head for 2 hours straight
European sprat
np
Sloth bearOP
thnks a lot man
European sprat
typescript didn't show there was an issue?
Sloth bearOP
but i am not using typescript
i should just use it
Pollock
BRUH
That's the solution 😂
Use TS
Step 1. Use TS!
Sloth bearOP
lol man now i know
European sprat
i just mentioned it because i saw page.tsx in your repro
Sloth bearOP
learning the hard way
Pollock
No worries, I was joking!
I've spent hours debugging something smaller than this before and it's definitely frustrating
I always just step away for a little
Sloth bearOP
yup that happens quite often