No error when I try to Import Server component into Client component.
Answered
darknight12345 posted this in #help-forum
![Avatar](https://cdn.discordapp.com/avatars/1015255707421003776/1776ecc192afbde0f24fa6b512ae3f54.webp?size=256)
darknight12345OP
This is my Navbar.tsx
export default Navbar
And this is my Navbarbar.client.tsx
As you know, when we don't put 'use client', next js knows it is server component.
So now I import server component into client component but it gives me no error.
'use client'
import { NavbarBrand } from './Navbar.client'
const Navbar = () => {
return (
<div className='sticky top-0 z-40 flex w-full items-center justify-between border-y border-borderColor bg-spaceBg px-3 py-2'>
<NavbarBrand />
</div>
</div>
)
}
export default Navbar
And this is my Navbarbar.client.tsx
import {
TbQuestionMark,
} from 'react-icons/tb'
export const NavbarBrand = () => {
return (
<div className='mr-4 flex cursor-default items-center space-x-2 rounded border border-borderColor px-3 py-1.5'>
<span className='text-neutral-600'>
<TbQuestionMark />
</span>
</div>
)
}
As you know, when we don't put 'use client', next js knows it is server component.
So now I import server component into client component but it gives me no error.
Answered by Alfonsus Ardani
server comps that are imported to client comps are automatically considered as client comps without any error
44 Replies
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
server comps that are imported to client comps are automatically considered as client comps without any error
Answer
![Avatar](https://cdn.discordapp.com/avatars/1015255707421003776/1776ecc192afbde0f24fa6b512ae3f54.webp?size=256)
darknight12345OP
You mean current my navtar.tsx is server component even if I define 'use client'?
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
Navbarbar.client.tsx is a client component even if you don't put
'use client'
![Avatar](https://cdn.discordapp.com/avatars/1015255707421003776/1776ecc192afbde0f24fa6b512ae3f54.webp?size=256)
darknight12345OP
I've learned it will cause error in next.js doc.
They upgrade it?
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
will it? if you don't use any server function, it will work fine as a client component
![Avatar](https://cdn.discordapp.com/avatars/1015255707421003776/1776ecc192afbde0f24fa6b512ae3f54.webp?size=256)
darknight12345OP
what do you mean server function?
fetch data?
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
like headers(), awaits, cookies(),
![Avatar](https://cdn.discordapp.com/avatars/1015255707421003776/1776ecc192afbde0f24fa6b512ae3f54.webp?size=256)
darknight12345OP
Next will automatically understand all components as server component and if we use 'use client',
It will treat it as a client compoent.
is it wrong?
It will treat it as a client compoent.
is it wrong?
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
yes you are correct
unless server component is imported into a client component, then that former component will be a client component
![Avatar](https://cdn.discordapp.com/avatars/1015255707421003776/1776ecc192afbde0f24fa6b512ae3f54.webp?size=256)
darknight12345OP
Can you please explain me in detail?
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
![Image](https://cdn.discordapp.com/attachments/1125098081516527696/1125109666075119727/image.png)
this <ExampleServerComponent /> will be regarded as Client components
![Avatar](https://cdn.discordapp.com/avatars/1015255707421003776/1776ecc192afbde0f24fa6b512ae3f54.webp?size=256)
darknight12345OP
So it's different from the docs?
Or the doc means, it will not cause an error but don't treat exampleservercomponent as server component?
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
well slightly, but thats just based on my experience
you might want to confirm with another, more experienced person haha
yes
![Avatar](https://cdn.discordapp.com/avatars/1015255707421003776/1776ecc192afbde0f24fa6b512ae3f54.webp?size=256)
darknight12345OP
Ok. Can you please help me just one more thing?
This is navbar.client.tsx
As you can see, in one tsx file, I defined two components.
One is used hook and one is not.
So for navbarpages component, it would be good if we import it as a server component.
When we import navbarpages in other component, will it be considered as a server component even if we use 'use client' in tsx file?
'use client'
export const NavbarPages = () => {
const pathname = usePathname()
return (
<ul className='flex items-center space-x-4 text-lg text-neutral-500'>
{pages.map((page) => {
const active = pathname === page.href
return (
<li
key={page.href}
className={classNames('rounded p-1.5 transition-colors', {
'bg-sky-500 text-sky-200 hover:bg-sky-400':
active,
'text-neutral-600 hover:bg-sky-700':
!active,
})}
>
<Link href={page.href}>
{ active ? page.selectedIcon : page.icon}
</Link>
</li>
)
})}
</ul>
)
}
export const NavbarUser = () => {
const { zero, user, dispatch } = useZero()
const handleSignout = useCallback(() => {
dispatch({
category: 'AUTH',
type: 'LOGOUT_START',
})
}, [user])
if (!zero.auth?.access_token) {
return (
<AuthModal>
<span className='flex cursor-pointer items-center space-x-2 rounded transition-colors hover:text-neutral-300'>
<TbLogin />
<span className='text-sm'>Login</span>
</span>
</AuthModal>
)
}
}
As you can see, in one tsx file, I defined two components.
One is used hook and one is not.
So for navbarpages component, it would be good if we import it as a server component.
When we import navbarpages in other component, will it be considered as a server component even if we use 'use client' in tsx file?
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
can you learn to use codeblocks as it makes it easier to read xD
![Avatar](https://cdn.discordapp.com/avatars/1015255707421003776/1776ecc192afbde0f24fa6b512ae3f54.webp?size=256)
darknight12345OP
How can I use that?
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
```js
paste code here
```
paste code here
```
![Avatar](https://cdn.discordapp.com/avatars/1015255707421003776/1776ecc192afbde0f24fa6b512ae3f54.webp?size=256)
darknight12345OP
I did. Cool.
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
you cant import navbarpages as server components
![Avatar](https://cdn.discordapp.com/avatars/1015255707421003776/1776ecc192afbde0f24fa6b512ae3f54.webp?size=256)
darknight12345OP
what do I have to do if I want to import navbarpages as server component?
Do I have to make that in another file?
Do I have to make that in another file?
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
you have to remove hooks and callbacks
![Avatar](https://cdn.discordapp.com/avatars/1015255707421003776/1776ecc192afbde0f24fa6b512ae3f54.webp?size=256)
darknight12345OP
If I do like that, navbaruser compoent will not be working.
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
then import navbarpages as client components
![Avatar](https://cdn.discordapp.com/avatars/1015255707421003776/1776ecc192afbde0f24fa6b512ae3f54.webp?size=256)
darknight12345OP
how about if I create that in another file?
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
yes you could that
probably i should tell you that "use client" will affect all components inside that file haha
![Avatar](https://cdn.discordapp.com/avatars/1015255707421003776/1776ecc192afbde0f24fa6b512ae3f54.webp?size=256)
darknight12345OP
Oh I see...
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
i thought thats 2 separate file already
![Avatar](https://cdn.discordapp.com/avatars/1015255707421003776/1776ecc192afbde0f24fa6b512ae3f54.webp?size=256)
darknight12345OP
How do you do in this situation?
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
create another file...
one for server/ssr another for client
![Avatar](https://cdn.discordapp.com/avatars/1015255707421003776/1776ecc192afbde0f24fa6b512ae3f54.webp?size=256)
darknight12345OP
in navbar.tsx
If I create navbar.server.tsx, navbar.client.tsx
And if we define server components in server.tsx and define client components in client.tsx,
do you think it is well structured?
If I create navbar.server.tsx, navbar.client.tsx
And if we define server components in server.tsx and define client components in client.tsx,
do you think it is well structured?
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
i dont think it matters what i think
i think what matters is if it does the job and if it works well for you
![Avatar](https://cdn.discordapp.com/avatars/1015255707421003776/1776ecc192afbde0f24fa6b512ae3f54.webp?size=256)
darknight12345OP
Okay. Thanks.
Thanks for your help.