Skip to article content
FrontendXLesson 38 / 40

Component Architecture and Composition

Build, explain and debug React 19 interfaces: components, rendering, state, effects, identity, forms and actions, server components, concurrency, performance and testing, ending with architecture and typing work on real component challenges.

advanced · about 55 minutes · 4 exercises

  • Predict
  • Implement
  • Debug
  • Interview

What you will be able to do

#
  • Compose behavior with children and slots instead of flags.
  • Separate container logic from presentational rendering.
  • Keep component APIs small and predictable.

Overview

#

Composition over configuration

A component with many boolean props that change its structure becomes hard to use. Passing children and named slots lets the caller decide the structure while the component owns the behavior.

snippet.tsx
<Card>
  <Card.Header>Title</Card.Header>
  <Card.Body>Content</Card.Body>
</Card>

Container and presentational

A container component handles data, effects and state. A presentational component takes props and renders. Separating them makes the presentational part easy to test and reuse, and keeps data logic in one place.

snippet.tsx
function UserCard() {
  const user = useUser();
  return <UserView user={user} />;
}

Compound components

A compound component shares implicit state through context. The parent provides the state and the children read it, so the caller composes the pieces without wiring props between them.

snippet.tsx
<Tabs defaultValue="a">
  <Tabs.List>
    <Tabs.Tab value="a">A</Tabs.Tab>
  </Tabs.List>
  <Tabs.Panel value="a">Panel A</Tabs.Panel>
</Tabs>

Small, predictable APIs

Fewer props with clear names are easier to learn. Prefer a single purpose per component and compose small pieces rather than adding a mode prop that switches between two components.

Pitfall: prop explosion

When a component grows a prop for every variation, it is usually two components in one. Split it or accept children so the caller controls the variation.

Pitfall: context for everything

Compound components use context to share state, but wrapping unrelated components in one provider couples them. Keep each compound component's context scoped to its own subtree.

Directory structure

Group by feature rather than by type. A feature folder holds its components, hooks and tests together, which keeps related code close and makes the feature easy to move.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Predict

    What does this component render when compact is true and showIcon is false?

    Read the code, then choose the best answer. The reference explanation stays hidden until you respond.
    stimulus.tsx
    <Card compact showIcon={false} title="A" subtitle="B" footer="C" />
    Choose the best answer
  2. Exercise 2Implement

    Refactor this flagged component into one that accepts children and named slots.

    starter.tsx
    function Card({ title, body, footer, showFooter }: { title: string; body: string; footer?: string; showFooter?: boolean }) {
      return <div><h2>{title}</h2><p>{body}</p>{showFooter && <footer>{footer}</footer>}</div>;
    }

    Reveal the reference answer
    reference.tsx
    function Card({ children }: { children: React.ReactNode }) {
      return <div>{children}</div>;
    }
    Card.Header = function Header({ children }: { children: React.ReactNode }) { return <h2>{children}</h2>; };
    Card.Body = function Body({ children }: { children: React.ReactNode }) { return <p>{children}</p>; };
    Card.Footer = function Footer({ children }: { children: React.ReactNode }) { return <footer>{children}</footer>; };
    
    // usage
    <Card>
      <Card.Header>Title</Card.Header>
      <Card.Body>Body</Card.Body>
      <Card.Footer>Footer</Card.Footer>
    </Card>

    Authored expected output: The caller composes the card from slots and controls which parts appear.

    The slots let the caller decide the structure and which parts to include, without a flag for every variation. The component owns layout and the caller owns composition.

  3. Exercise 3Debug

    This component is hard to reuse because it fetches and formats and renders. Explain and fix.

    starter.tsx
    function UserList() {
      const [users, setUsers] = useState([]);
      useEffect(() => { fetch('/api/users').then((r) => r.json()).then(setUsers); }, []);
      return <ul>{users.map((u) => <li>{u.first} {u.last}</li>)}</ul>;
    }

    Reveal the reference answer
    reference.tsx
    function useUsers() {
      const [users, setUsers] = useState<User[]>([]);
      useEffect(() => { fetch('/api/users').then((r) => r.json()).then(setUsers); }, []);
      return users;
    }
    
    function UserList({ users }: { users: User[] }) {
      return <ul>{users.map((u) => <li key={u.id}>{u.first} {u.last}</li>)}</ul>;
    }
    
    function UserListContainer() {
      return <UserList users={useUsers()} />;
    }

    Authored expected output: Data fetching lives in a hook and rendering lives in a presentational component.

    Separating the container logic from the presentational rendering makes the list reusable with any data and testable without a network. The hook can be reused elsewhere.

  4. Exercise 4Interview

    A shared component has grown twelve boolean props. Explain your approach to fixing it.

    starter.tsx
    // many flags, conditional structure

    Reveal the reference answer
    reference.tsx
    Twelve booleans usually mean the component is several components in one. I would identify the distinct layouts the flags select and either split them into separate components or, better, replace the flags with composition: children and named slots so the caller decides the structure. Behavior stays in the component; structure moves to the caller. I would keep the public API small, one purpose per component, and group the feature's files together. The result is fewer props, clearer intent and easier testing.

    Authored expected output: A plan to split or compose, replacing flags with children and slots and keeping the API small.

    The answer should diagnose flag explosion and prescribe composition, which removes the combinatorial growth of boolean props.

Common misconceptions

#
  • More props make a component more flexible. Many boolean props usually mean the component should be split or accept children.
  • Presentational components cannot have any state. They can have local UI state; they just do not own data fetching.
  • Compound components need prop wiring. They share state through scoped context.
  • Grouping files by type is best. Grouping by feature keeps related code together.

Follow-up practice

#
  • react-typescript-component-typing
  • react-compiler-and-purity

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.