Reputation: 639
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
Upvotes: 48
Views: 75866
Reputation: 9
I think whenever you change a component from server to client or vice versa , you have to rerun pnpm run dev . May be it is related to build of the app.
Upvotes: 0
Reputation: 1
My problem was resolved by adding 'use client' to the parent component tsx file.
Upvotes: 0
Reputation: 1
I was also stucked with this thing. After hours of work i just stopped my server and started it again and all worked good. The error was not there by doing so
Upvotes: 0
Reputation: 81
Had the same problem while going through NextJS Dashboard tutorial(adding Active Links using usepathname hook and 'use client' section).
Restarting the npm
or pnpm
server resolved the issue!
Upvotes: 6
Reputation: 131
After stopping the server and running pmpm run dev
, the issue was resolved successfully.
Upvotes: 13
Reputation: 10472
I did the default export thing and it still wasn't working for me.
I gave up and turned off my computer, when I returned, it was working.
So based on that, here are things to try doing in order:
npm run dev
commandUpvotes: 18
Reputation: 1441
My problem was resolved by changing import { SomeType }
to import type { SomeType }
.
Upvotes: -1
Reputation: 1120
If you are exporting your component as default export and are importing your component as a named export or vice versa, this could also be the scenario where the problem occurs
Importing and Exporting error like this:
exported component:
export default function ExportedComp() {
//logic
}
Page where you import:
import {ExportedComp} from 'pathname'
Or vice versa!
Upvotes: 3
Reputation: 336
I ran into this issue as well, my problem was the way I was exporting / importing.
This is for a react component library. I was receiving your same error when importing my component from the package in my next.js application.
├── src
│ ├── components
│ │ ├── MyComponentName
│ │ │ ├── MyComponentName.tsx
│ │ │ ├── ...
│ │ │ ├── index.ts
│ │ ├── index.ts
│ ├── ...
│ ├── index.ts
├── ...
MyComponentName.tsx
const MyComponentName () => {
return <>
/* stuff */
</>
};
export default MyComponentName; /* make sure to default export initially */
MyComponentName/index.ts
import MyComponentName from "./MyComponentName"; // import the component from the component file
export default MyComponentName; // export default
components/index.ts
export { default as MyComponentName } from "./MyComponentName";
src/index.ts
export { MyComponentName } from "./components";
Then, to import from the package:
import { MyComponentName } from "@clxrityy/my-component-library";
I know this particular example seems like an overly complex structure of imports/exports, but it's necessary for my purposes. I hope this can help anyone who is running into a similar issue with these circumstances.
Upvotes: 1
Reputation: 49561
I had this return jsx:
return <>{product && <ProductView product={product} />}</>;
it turns out that importing one component inside ProductView
was from the wrong path.
Upvotes: 0
Reputation: 262
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'
Upvotes: 7
Reputation: 336
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!
Upvotes: 30