#들어가며

Compound Component 패턴은 React에서 관련 컴포넌트들을 하나로 묶어 사용하기 좋은 패턴이다. Dialog.Root, Dialog.Trigger, Dialog.Content처럼 점 표기법으로 연관 컴포넌트를 그룹화할 수 있다. Radix UI나 Chakra UI 같은 라이브러리에서 흔히 볼 수 있는 패턴이다.

그런데 이 패턴을 Next.js의 Server Component에서 사용하면 이상한 일이 벌어진다. 분명 클라이언트 컴포넌트에서는 잘 동작하던 NavigationBar.Root가 Server Component에서는 undefined가 된다. 왜 같은 import인데 동작이 다를까?

#문제 상황

먼저 일반적인 Compound Component 구현을 보자.

tsx
// NavigationBar.tsx
"use client";

function NavigationBarRoot({ children }) {
  return <nav className="navigation">{children}</nav>;
}

function NavigationBarBar({ children }) {
  return <div className="navigation-bar">{children}</div>;
}

function NavigationBarBrand({ children }) {
  return <span className="navigation-brand">{children}</span>;
}

const NavigationBar = Object.assign(NavigationBarRoot, {
  Root: NavigationBarRoot,
  Bar: NavigationBarBar,
  Brand: NavigationBarBrand,
});

export default NavigationBar;

Object.assign으로 함수 객체에 서브 컴포넌트들을 속성으로 붙이는 방식이다. 클라이언트 컴포넌트에서는 완벽하게 동작한다.

tsx
// ClientPage.tsx
"use client";
import NavigationBar from "@/components/NavigationBar";

export default function ClientPage() {
  return (
    <NavigationBar.Root>
      <NavigationBar.Bar>
        <NavigationBar.Brand>My App</NavigationBar.Brand>
      </NavigationBar.Bar>
    </NavigationBar.Root>
  );
}

하지만 layout.tsx 같은 Server Component에서 똑같이 사용하면 문제가 생긴다.

tsx
// layout.tsx (Server Component)
import NavigationBar from "@/components/NavigationBar";

export default function Layout({ children }) {
  return (
    <NavigationBar.Root>
      {" "}
      {/* undefined! */}
      <NavigationBar.Bar />
    </NavigationBar.Root>
  );
}

NavigationBar.Root가 undefined다. 에러 메시지는 "Cannot read properties of undefined"다.

#왜 이런 일이 발생하는가?

다이어그램을 불러오는 중입니다.Server Component의 모듈 참조 방식 · 원본 다운로드

핵심은 Server Component가 클라이언트 모듈을 직접 실행하지 않는다는 점이다.

Server Component는 서버에서 렌더링된다. 클라이언트 모듈("use client"가 붙은 파일)을 import할 때, Next.js는 해당 모듈을 실제로 실행하지 않고 **모듈 참조(Module Reference)**만 생성한다. 이 참조는 "이 컴포넌트는 클라이언트에서 렌더링해야 한다"는 정보를 담은 placeholder 같은 것이다.

문제는 이 참조가 정적 export만 인식한다는 점이다. Object.assign은 런타임에 객체에 속성을 동적으로 추가하는 메서드다. 정적 분석 시점에는 NavigationBar 함수 객체에 .Root, .Bar, .Brand라는 속성이 존재하는지 알 수 없다.

text
# 정적 분석 시점 (빌드 타임)
export default NavigationBar  →  함수 하나만 보임

# 실제 런타임
NavigationBar.Root = NavigationBarRoot  →  이건 런타임에야 할당됨

결과적으로 Server Component에서 NavigationBar.Root에 접근하면, 모듈 참조에는 해당 속성이 없으므로 undefined가 반환된다.

#잘못된 해결책

가장 쉬운 해결책처럼 보이는 건 layout.tsx에 "use client"를 붙이는 것이다.

tsx
// layout.tsx
"use client"; // 이렇게 하면 동작한다, 하지만...

import NavigationBar from "@/components/NavigationBar";
// ...

동작은 한다. 하지만 이 방식은 layout 하위의 모든 컴포넌트가 클라이언트 번들에 포함된다는 의미다. Server Component의 이점인 서버 사이드 렌더링, 작은 번들 크기, 데이터 페칭 최적화를 모두 포기하는 셈이다. 단순히 compound 패턴을 쓰고 싶다는 이유로 전체 레이아웃을 클라이언트로 내리는 건 과도한 비용이다.

#올바른 해결책: Named Export 병행

해결책은 간단하다. 서브 컴포넌트들을 named export로도 내보내면 된다.

tsx
// NavigationBar.tsx
"use client";

function NavigationBarRoot({ children }) {
  return <nav className="navigation">{children}</nav>;
}

function NavigationBarBar({ children }) {
  return <div className="navigation-bar">{children}</div>;
}

function NavigationBarBrand({ children }) {
  return <span className="navigation-brand">{children}</span>;
}

// Named exports - Server Component에서 사용 가능
export { NavigationBarRoot, NavigationBarBar, NavigationBarBrand };

// Compound pattern - Client Component에서 사용 가능
const NavigationBar = Object.assign(NavigationBarRoot, {
  Root: NavigationBarRoot,
  Bar: NavigationBarBar,
  Brand: NavigationBarBrand,
});

export default NavigationBar;

다이어그램을 불러오는 중입니다.두 가지 사용 방식 · 원본 다운로드

Named export는 정적 분석 시점에 모듈의 어떤 것들이 export되는지 명확히 알 수 있다. Server Component에서 이 모듈을 import하면 NavigationBarRoot, NavigationBarBar, NavigationBarBrand가 각각 별도의 모듈 참조로 인식된다.

Server Component에서는 named export를 사용한다.

tsx
// layout.tsx (Server Component)
import {
  NavigationBarRoot,
  NavigationBarBar,
  NavigationBarBrand,
} from "@/components/NavigationBar";

export default function Layout({ children }) {
  return (
    <NavigationBarRoot>
      <NavigationBarBar>
        <NavigationBarBrand>Tech Blog</NavigationBarBrand>
      </NavigationBarBar>
      {children}
    </NavigationBarRoot>
  );
}

Client Component에서는 기존처럼 compound 패턴을 사용할 수 있다.

tsx
// ClientPage.tsx
"use client";
import NavigationBar from "@/components/NavigationBar";

export default function ClientPage() {
  return (
    <NavigationBar.Root>
      <NavigationBar.Bar />
    </NavigationBar.Root>
  );
}

#사용 규칙 정리

Server Component에서는 named export만 사용하고, Client Component에서는 compound 패턴을 사용한다. 이렇게 역할을 분리하면 런타임 참조 문제 없이 두 가지 방식을 모두 유지할 수 있다.

참고로 Radix UI 같은 라이브러리들도 이런 방식으로 dual export를 제공한다. @radix-ui/react-dialog를 보면 Dialog.Root도 쓸 수 있고, Root를 직접 import할 수도 있다. Server Component와의 호환성을 고려한 설계다.

#정리

Server Component는 클라이언트 모듈을 직접 실행하지 않고 모듈 참조만 생성한다. 이 참조는 정적 export만 인식하기 때문에 Object.assign으로 런타임에 붙인 속성은 볼 수 없다. 해결책은 서브 컴포넌트들을 named export로 함께 내보내는 것이다. 이렇게 하면 Server Component에서는 named export를, Client Component에서는 compound 패턴을 사용할 수 있다.