NextJS 13: does not work onClick but the component uses 'use client;' – Onclick

by
Alexei Petrov
next.js next.js13 onclick onselect reactjs

Quick Fix: Roll back to Next.js version 13.4.16 to resolve the issue with use client and onClick functionality. This solution addresses the reported bug until the latest version is patched.

The Problem:

In a Next.js 13 application, you are trying to implement a select component using the "use client" hook for the component. However, the onClick function defined within the component does not seem to be working. You are using the handleOnClick function to handle the onClick event on the options within the select component, but for some reason, it is not triggering. You have already imported the necessary dependencies and defined the handleOnClick function correctly.

The Solutions:

Solution 1: Downgrade Next.js to version 13.4.16

Downgrading Next.js to version 13.4.16 may resolve the issue. This is because there’s a known bug in the latest version (13.4.17) that causes the onClick event handler not to work correctly when using the use client hook.

To downgrade Next.js, you can run the following command:

npm install [email protected]

After downgrading, restart your development server and check if the onClick event handler is working as expected.

Note: Keep an eye on the Next.js GitHub issue tracker for updates on this bug. Once a fix is released, you can upgrade to the latest version of Next.js again.

Solution 2: Update Node Version

To resolve the issue with the onClick function not working, consider updating your Node.js version. This action has been reported to resolve similar problems.

Q&A

What should we do to fix this issue?

Update you node version or you can downgrade your version to 13.4.16.

Video Explanation:

The following video, titled "Next.js Event handlers cannot be passed to client component ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

Professional JavaScript Course: https://www.udemy.com/course/professional-javascript-course/?referralCode=0C1D5752923168BC87C2 ...