Next.js server component with react-stately Item component #5030
-
I am using a ComboBox component (created with useComboBox) and passing it a group of Item components from react-stately. The component throws an error "Error: Unknown element <[object Object]> in collection." on the useComboBoxState hook, unless the parent component has "use client". useComboBoxState is only used inside the ComboBox component which itself is a client component. Can Item and Section react-stately components be rendered on the server? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
We do render them in SSR in some tests. However, I don't know the difference between our tests and NextJS. https://github.com/adobe/react-spectrum/blob/main/packages/%40react-spectrum/listbox/test/ListBox.ssr.test.js though the fact that we don't have an SSR test for our React Spectrum component worries me. You may find this PR of interest #4913, it uses a newer, more flexible collection we'll eventually have documented everywhere. |
Beta Was this translation helpful? Give feedback.
-
Collection components like Item must be rendered from a client component, not a server component. You must have "use client" in the file where you render The reason is that we traverse the JSX tree to find the items, specifically looking for elements with As Rob mentioned, in the future we'll have a new implementation of collections that works better with server components. However, that's not publicly exported yet since we are still working on it. So in the meantime, the solution is just to render the |
Beta Was this translation helpful? Give feedback.
Collection components like Item must be rendered from a client component, not a server component. You must have "use client" in the file where you render
<Item>
(or in a file above that).The reason is that we traverse the JSX tree to find the items, specifically looking for elements with
element.type === Item
. However, server components / Next.js rewrites these to beReact.lazy
when they are rendered in a server component. That makes it impossible for us to tell what the actual type of the element is, hence the error you see here.As Rob mentioned, in the future we'll have a new implementation of collections that works better with server components. However, that's not publicly exported …