Nextjs client-side rendering not working?

1.4k Views Asked by At

I have created a project in nextjs 13 using app router. I want to acheive client-side rendering. In their documentation it is mentioned that for rendering on client-side, add 'use client' at the top of file and that will be rendered on client-side. but unfortunately i am not able to have its effects of client-side. i am stuck with that.

This is my page.js file:

'use client';

import { useState } from 'react'

export default function Counter() {
  const [count, setCount] = useState(0)
  console.log("counter")

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>Click me</button>
    </div>
  )
 }

It should be rendered on client-side but it's not.

4

There are 4 best solutions below

0
Muhammad Afshal On BEST ANSWER

i fixed that issue, actually i installed nodejs updated version with old nvm version. i updated my nvm version and install nodejs, and it worked fine.

6
Abdur Rehman On

Migrate your app/page.js to pages/index.js, and see if it works. Thanks.

10
jimpo26 On

What do you mean with "It should be rendered on client-side but it's not."? Do you receive the following error: "You're importing a component that needs useState. It only works in a Client Component but none of its parents are marked with "use client", so they're Server Components by default. Learn more: https://nextjs.org/docs/getting-started/react-essentials"? if not, and the counter increses when you click the button, the component is rendered on client side

Also, are you using the latest version of Next.JS?

4
Abdur Rehman On

If you have <main></main> tag in the layout.js file change it to <body></body and see if this resolves your issue. If not tell me, how you call your onClick function.

Another Solution: Try updating the nodejs and nextjs by running :

yarn add next@latest react@latest react-dom@latest

Thanks