Category: Next.js, React and TypeScript

Next.js: Event handlers cannot be passed to Client Component props

Read this guide in Polish

An error appears when rendering onClick from Server Component to the client component. Browser event handling functions must exist in client context; no callback can be serialized across the server-client boundary.

How to determine the source of the error

Find a component with the passed onClick, onChange or function in the props object.

Check the location of the use client directive and whether the imported component is actually a customer boundary.

What to do step by step

1. Move an interactive button and handler to a small Client Component while retaining the data download on the server side.

2 . Transfer to the customer only serialized data and identifiers necessary for the event.

How to check the score

Build ends without serialization error, and clicking continues to trigger the expected action.

What to do

Do not add the use client to the entire layout just because one button has onClick.