Error when manually closing a headlessui dropdown menu #3882
-
|
I'm building a Next.js app using the headlessui library. I want each menu item to be a Next , and I want the menu to close when clicked. The documentation explains that "some third-party Link components use event.preventDefault() which prevents the menu from closing", and gives sample code of how to solve the issue. I copied the sample code exactly, only changing to . However, when I run the code, I get a react error: "Functions are not valid as a child of Client Components." Code: Error message:
package.json node version: v24.18.0 Am I missing something obvious? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
In the App Router this component is a Server Component unless the file starts with Make the menu component a Client Component: 'use client'
import { Menu, MenuButton, MenuItem, MenuItems } from '@headlessui/react'
import Link from 'next/link'
export function NavMenu() {
return (
<Menu>
<MenuButton>Terms</MenuButton>
<MenuItems anchor="bottom">
<MenuItem>
{({ close }) => (
<Link href="/" onClick={close}>
Read and accept
</Link>
)}
</MenuItem>
</MenuItems>
</Menu>
)
}Or put just this menu in a separate client file and import it from your page/layout. The Headless UI example is fine; the missing bit is that |
Beta Was this translation helpful? Give feedback.
In the App Router this component is a Server Component unless the file starts with
'use client'.MenuItem's child is a render-prop function, and Next cannot serialize that function across the Server -> Client Component boundary, which is what the error is complaining about.Make the menu component a Client Component: