14

I ran into some weird behavior in Next js 13 while trying to do a simple action. I have prepared the most simple example showing the problem. I hope you can help me figure out why this is happening.

There are 3 components.

page:

import {Container} from '../components/Container'
export default function index() {
  return (
    <Container>
        <Container.Window>
            <h1>12345</h1>
        </Container.Window>
    </Container>
  )
}

Container:

'use client';
import { Window } from './Window';

export const Container = ({ children }) => {
  return children;
};

Container.Window = Window;

Window:

"use client";
export const Window = ({children})=>{
  return children
}

Page server component. The Container and Window components are client-side. Container imports and exports Window.

I am getting this error:

"Unsupported Server Component type: undefined"

  • Import the Window component separately.
    Works, no errors

  • Make the Container component a server component.
    Works, no errors

  • Make the page component client-side.
    Works, no errors

But I think the first option is the most convenient. Would love to make it work

https://stackblitz.com/edit/nextjs-fwuztm?file=app%2Fpage.js,components%2FContainer.js,components%2FWindow.js - example

TylerH
  • 20,799
  • 66
  • 75
  • 101
Roman
  • 175
  • 2
  • 3
  • 15

2 Answers2

8

I had the same issue. I was able to fix by changing the named export to a default export, and importing without the braces.

Try changing:

export const Window = ({children})=>{

to

const Window = ({children})=>{
...
}
export default Window;

Hopin that helps!

Awyssa
  • 116
  • 6
1

Update (for named imports)

As per Jason Frank's comments, you can keep the 'use client' at the component level without the need to mark all components as client side in the index.ts. This allows you to keep using named imports whilst keeping a folder of mixed server/client components.

// Client Side Component
'use client'

export default function ClientCard() {
  return <p>client side card here</p>
}
// Server Side Component
export default function ServerCard() {
  return <p>server side card here</p>
}
// index.ts
export { default as ClientCard } from './client-card';
export { default as ServerCard } from './server-card';

Not too sure if this is related but ran into a similar issue.

'use client'

export function Card() {
  return <p>card here</p>
}
// index.ts
export * from './card'

The above threw errors when attempting to import via import { Card } from './card.

Adding 'use client' to the index.ts seemed to fix the issue.

// index.ts - FIXED
'use client'

export * from './card'
lu_ke____
  • 143
  • 9
  • While this approach did solve the error (in my monorepo), my testing showed that it also made (*all*) the components exported from the `index.ts` file *client* components. – Jason Frank Apr 10 '23 at 00:58
  • Unfortunately, that is the case. I have ended up separating my server and client components into separate folders. I don't mind it as it means I'm consciously keeping the client/server logic separate. I much prefer the named exports over a default export so I'll be sticking with this for now. – lu_ke____ Apr 10 '23 at 02:46
  • 1
    I was able create a named-export approach at the `index.ts` level by writing each export line there like: `export { default as Anchor } from "./Anchor";` So for consuming projects in my monorepo, they still use named imports like: `import { Anchor } from "ui" // "ui" is a package in my monorepo` – Jason Frank Apr 10 '23 at 21:23