Vibe coding with Claude Code course ยท Module 3: Twitter clone

The chest and its cargo: children and component composition

8 min read ยท
In this lesson8

The feed counts likes and accepts new posts but it has three small flaws. The "Open post" link hangs below the post frame instead of inside it, because Post cannot accept anything to put inside. The same frame, the classes rounded-lg border border-gray-300 p-4, is copied into Post and into PirateCard, so changing the corner radius would mean editing two files. And the feed page and the post page spell out four props from one object by hand: author={post.author}, avatar={post.avatar} and so on.

The chest does not ask about its cargo

From lesson 2_2 you know props: the parent writes them like attributes, and the component reads them. There is one prop you do not write as an attribute: children. It is everything the parent puts between the opening and the closing tag of a component. You have met it in lesson 2_1: {children} in app/layout.tsx is the place where Next.js drops the current page.

Think of a chest on deck. The chest has walls, a lock and fittings, but it does not ask what you will put inside: the one who packs decides about the cargo. A component with children is such a chest. Building larger parts of the interface from small components that accept someone else's content is called composition.

Request for Claude Code

Today's task is a refactor, a change in how the code is built without changing what the player sees. There is one exception, named outright: the link moves into the frame. All the decisions are in the request, so Manual mode is enough, as in lesson 2_2.

Request for Claude Code:

Refactor the messenger without changing how it looks. 1) Extract the
frame of a post and of a card into components/Card.tsx: a Card
component with a children prop. Use it in @components/Post.tsx and
@components/PirateCard.tsx. 2) Post gets an optional children, placed
below the like button. In @app/feed/page.tsx move the "Open post" link
inside Post. 3) In @app/feed/page.tsx and @app/post/[id]/page.tsx pass
the post data with {...post}. No "use client" in Card. At the end run
npm run build and npm run lint, and show both results.

The request splits the work into three numbered steps, so in the diff you check each one separately. The phrase "without changing how it looks" is a test for you: after the change the feed should look the same, with the one difference you asked for.

What Claude wrote: the chest

A new file appeared in the Explorer, a shared chest for posts and crew cards. Open components/Card.tsx:

interface CardProps {
  children: React.ReactNode;
}

export default function Card({ children }: CardProps) {
  return <article className="rounded-lg border border-gray-300 p-4">{children}</article>;
}

React.ReactNode is the type of everything that can stand in JSX between tags: text, a number, an element, another component, even nothing. The card only draws the frame and puts {children} inside, without knowing whether it carries a post or a pirate card. There is no state and no events here, so the file stays a Server Component, without the directive.

What Claude wrote: a post with room for cargo

Now components/Post.tsx. The props interface got two new question marks, which means two new optional fields:

interface PostProps {
  author: string;
  avatar: string;
  content: string;
  likes?: number;
  time?: string;
  children?: React.ReactNode;
}

children? is optional, because the post page and the captain's posts from the form put nothing inside. The question mark on likes is Claude's idea: it noticed that Feed writes likes={0} for every captain's post, and turned that into a default value. You can see it in the function arguments:

export default function Post({ author, avatar, content, likes = 0, time, children }: PostProps) {

The notation likes = 0 is a default value: when the parent does not pass the prop, the component gets 0. When it does pass one, say 12, the default does not matter. Thanks to this Feed no longer has to write likes={0}, and the number type from lesson 2_2 still makes sure nobody passes text.

Instead of its own <article>, the body of the component now uses the chest:

<Card>
  {/* avatar, author and time unchanged */}
  <p className="mt-3">{content}</p>
  <LikeButton initialLikes={likes} />
  {children}
</Card>

Everything between <Card> and </Card> reaches the card as its children. Below the like button stands the post's own {children}: a place for the cargo that the page will bring. PirateCard went through the same change: the <article> with the frame classes became a <Card>, and the inside of the card did not change by a single letter.

Cargo from outside and the spread

The most interesting change waits in app/feed/page.tsx, in the map loop over the crew's posts:

<Post {...post}>
  <Link href={`/post/${post.id}`} className="mt-2 block text-sm text-blue-600 underline">
    Open post
  </Link>
</Post>

{...post} is a props spread: instead of writing author={post.author}, avatar={post.avatar} and so on, you pass every field of the object as a separate prop. It is the same ... operator that copied the array of posts in lesson 2_3. Post does not end here with />, it has a pair of tags, and the Link between them becomes its children.

The post still knows nothing about addresses: what lies in the frame is decided by the feed page. If Post got an href prop and passed it down through more components just so that it reached the link, that would be passing props deep down the tree, called props drilling. Composition spares you this relay race. The post page got shorter, to <Post {...post} />, without cargo.

What you do not need here

In older tutorials, and sometimes in Claude's proposals, you will see heavier patterns: compound components that share state through context, or a function in a prop that draws the content itself (render props). They solve problems the messenger does not have. I suggest a simple rule: if Claude proposes context or a new pattern for a frame with a single piece of content, ask what problem it solves before you agree.

Captain's review

Open /feed, /post/2 and /profile in the browser, then the diff in Source Control:

  1. The look is unchanged. Frames, colours and spacing are as before the refactor, and the only difference is the link inside the frame. A typical agent mistake in a refactor is changes "while at it": different classes, new prop names across several files. Every diff line that does not follow from the three steps of the request is a question for Claude.
  2. The cargo really lands. If Claude adds children to the interface but forgets to place {children} in the JSX, the build passes and the link silently vanishes from the feed. The only trace is the warning 'children' is defined but never used in the npm run lint output, which is why you read the whole output, not only the last line.
  3. The spread passes everything. {...post} also sends id to Post, which the post does not use. Here it is harmless, because Post pulls only its own fields out of the props. Be careful when Claude spreads a data object straight onto an HTML tag, for example <article {...post}>: then the data fields land in the HTML as attributes.
  4. The type of the cargo. children has the type React.ReactNode. If Claude wrote string, the build would stop on the error Type 'Element' is not assignable to type 'string', because Link is an element, not text.

Next step

When everything matches, ask for a commit with a message like "Card with children, Post through spread, post link inside the frame". The chest will be useful right away in the next place, without changing its code:

In @app/profile/page.tsx, below the paragraph with the role, add an
"About the captain" card with an h2 heading and two sentences about
Redbeard, built from Card. Do not change components/Card.tsx. Run
npm run build.

The <h2> heading will reach the card as part of the cargo. If Claude wants to add a new title prop to Card, ask why: a chest that accepts any content does not need a separate compartment for a title. In the editor below waits a feed made of the same blocks: remove {children} from the post or the likes field from one object and see what changes.

Remember: the chest does not ask about its cargo, and the captain checks that the cargo really got inside.

Code for this lesson: App.jsx
// The feed and the profile built from blocks: Card is the chest, children is the cargo.
// Try it: remove {children} from components/Post.jsx or the likes field from a post.
import Post from './components/Post';
import PirateCard from './components/PirateCard';

const posts = [
  { id: '1', author: 'Anne Bonny', avatar: 'AB', content: 'Kraken spotted near Skull Island. Sailing west.', likes: 12 },
  { id: '2', author: 'Jack Rackham', avatar: 'JR', content: 'Rudder fixed. Who will help with the sails?', likes: 5 },
  // No likes field: Post will use the default value 0
  { id: '3', author: 'Mary Read', avatar: 'MR', content: 'Fish soup for dinner. First come, first served!' },
];

const following = [
  { id: '1', name: 'Anne Bonny', role: 'Captain', avatar: 'AB', description: 'She was the first to spot the kraken.' },
  { id: '2', name: 'Jack Rackham', role: 'Helmsman', avatar: 'JR' },
];

export default function App() {
  return (
    <div className="min-h-screen bg-gray-50 p-6 text-gray-900">
      <h1 className="text-3xl font-bold">Crew feed</h1>
      <ul className="mt-4 space-y-4">
        {posts.map((post) => (
          <li key={post.id}>
            {/* Spread: every field of the post object goes in as a separate prop */}
            <Post {...post}>
              {/* Cargo: everything between the tags goes into children */}
              <a href={'#/post/' + post.id} className="mt-2 block text-sm text-blue-600 underline">
                Open post
              </a>
            </Post>
          </li>
        ))}
      </ul>

      <h2 className="mt-10 text-xl font-bold">Pirates you follow</h2>
      <div className="mt-4 grid grid-cols-1 gap-4 md:grid-cols-2">
        {following.map((pirate) => (
          <PirateCard key={pirate.id} {...pirate} />
        ))}
      </div>
    </div>
  );
}

Spotted a mistake in this lesson?

Check yourself

Answer the questions from this lesson. Pick an answer to see right away whether it is correct.

  1. 1. What is the children prop in the Post component?

  2. 2. The post object has the fields id, author, avatar, content and likes. What reaches the component when you write <Post {...post} />?

These are 2 of 3 questions for this lesson. Solve the rest in the game.

Hands-on tasks in the game

  • Horizontal ordering

    Arrange the tokens that pass all the fields of a post to the Post component:

  1. QuestionThe children prop
  2. QuestionProps spread
  3. Put in orderProps spread in JSX
  4. QuestionA default prop value

Useful articles