Question about client server components rendering
Unanswered
Cape lion posted this in #help-forum
Cape lionOP
In the following example the file is \app\users[id]\page.jsx and the code in this file is:
When I visit the route /users/user1?name=John
The in the browser appears :
Params = user1
searchParams = John
and the result of the logs:
{ id: 'user1' }
{ name: 'John' }
appears both in the terminal and in the console of my browser.
Why this is happening? I write in the first line "use client"; and I didn't expected for the logs to appear in my terminal but only in the client.
Does this mean that in client components the template of the page is rendered on the server and then I can use all the features of client components?
Note: If I don't write "use client"; in the top of the file, the logs doesn't appear in the browser console (as I expected)
Edit: when I navigate in this route ( /users/user1?name=John), through the app via a next/link the logs appear only in my browser console and in the terminal, but when I paste the url in the browser adress and hit enter the logs appear in both browser console and terminal (why??)
"use client";
const UserPage = ({ params, searchParams }) => {
console.log(params);
console.log(searchParams);
return (
<div>
Params = {params.id}
<br />
searchParams = {searchParams.name}
</div>
);
};
export default UserPage;When I visit the route /users/user1?name=John
The in the browser appears :
Params = user1
searchParams = John
and the result of the logs:
{ id: 'user1' }
{ name: 'John' }
appears both in the terminal and in the console of my browser.
Why this is happening? I write in the first line "use client"; and I didn't expected for the logs to appear in my terminal but only in the client.
Does this mean that in client components the template of the page is rendered on the server and then I can use all the features of client components?
Note: If I don't write "use client"; in the top of the file, the logs doesn't appear in the browser console (as I expected)
Edit: when I navigate in this route ( /users/user1?name=John), through the app via a next/link the logs appear only in my browser console and in the terminal, but when I paste the url in the browser adress and hit enter the logs appear in both browser console and terminal (why??)
11 Replies
because everything is still pre-rendered on the client. if you logged it inside of a useEffect hook it would only log once its on the client.
No matter what its pre-rendered on the server
then hydration happens
@Jboncz because everything is still pre-rendered on the client. if you logged it inside of a useEffect hook it would only log once its on the client.
i think you mean pre-rendered on the server
@Jboncz https://nextjs.org/docs/app/building-your-application/rendering/client-components#full-page-load
Cape lionOP
Thanks a lot! Now I have a better understanding, but I still have s question.
According to the link you shared if there is subsequent navigation the component is rendered entirely on the client, without server rendered html. In my example, how does client has access to params and searchParams ( the logs happen on the client)? I though that those props are available only on the server.
According to the link you shared if there is subsequent navigation the component is rendered entirely on the client, without server rendered html. In my example, how does client has access to params and searchParams ( the logs happen on the client)? I though that those props are available only on the server.
You can use the hooks useSearchParams and useParams to get those in client components
@chisto You can use the hooks useSearchParams and useParams to get those in client components
Cape lionOP
Of course. I wonder if those hooks are completely useless, because we can use params and searchParams parameters