Error in Next.js tutorial with event handlers in server components.
Add 'use client' at the very top of your component file (before any imports):
'use client'
import Image from 'next/image'
// ... other imports
export default function YourComponent() {
// your component code with onClick handlers
}Why This Happens:
In Next.js 13+ with the App Router, all components are React Server Components by default. Server Components render on the server and can't include interactive JavaScript like event handlers (onClick, onChange, etc.).
When you add 'use client' at the top of the file, you're telling Next.js to treat this as a Client Component, which runs in the browser and can handle user interactions.
Best Practice:
If you're working with Sanity and Next.js, you typically want to:
- Keep data-fetching components as Server Components (no
'use client') - Add
'use client'only to components that need interactivity (buttons, forms, state management) - Consider splitting your component: keep the data-fetching part as a Server Component and extract the interactive UI (like your login/logout buttons) into a separate Client Component
For your specific code, since you have onClick handlers for connectWallet() and disconnectWallet(), the entire component needs to be a Client Component, so add 'use client' at the top of the file.
This is a common gotcha when you're new to Next.js 13+ and the App Router. The 'use client' directive is how you opt into client-side interactivity!
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.