Vibe coding with Claude Code course ยท Module 3: Twitter clone
The captain's seal: the Post and PirateCard components
In this lesson9
The route map stands but the feed shows only the author and the content for now. A post is supposed to get a round avatar and a like button, and in two places: on the feed and on the page of a single post. The profile, in turn, should show the cards of the pirates you follow. If every page kept its own copy of the same JSX, changing the colour of the like button would mean edits in several files and several chances for a mistake.
A component: one seal, many impressions
React solves this problem with components. A component is a function that returns JSX, a description of a piece of the interface, and when it has nothing to show, it returns null. React will not draw a plain data object, for example { name: "Redbeard" }. A component name starts with a capital letter, which is how React tells <Post /> (your function) from <div> (an HTML tag).
Think of the captain's seal: there is one seal, and there can be any number of impressions on the lists. The data in which the impressions differ is passed through props. They work like function arguments: the parent writes them like HTML attributes, and the component only reads them. TypeScript describes them with an interface from lesson 1_4, so a typo in a prop name or text instead of a number ends in an error at build, not in an empty card on the player's screen.
Request for Claude Code
The task touches five files but leaves no decisions to make: names, props and places of use are in the request. The Manual mode, where you approve every edit, is enough. When a request leaves choices open, start with /plan, as in lesson 2_1.
Request for Claude Code:
Extract two components from the messenger, each in its own file in
components/. Post: props author, avatar, content (strings) and likes
(a number), with a round avatar and a like button that for now only
shows the number. Use it in @app/feed/page.tsx and in
@app/post/[id]/page.tsx. PirateCard: props name, role, avatar and an
optional description. On @app/profile/page.tsx show a list of three
followed pirates under the role paragraph. Describe the props with an
interface in the component file. No "use client" and no state. Do not
touch components/crew.tsx, because CrewCard belongs to the landing
page. At the end run npm run build and npm run lint.The request names the files, the props with their types and the places of use, and the prohibitions say what not to touch. The last sentence demands proof, not a report saying "done".
What Claude wrote: PirateCard
Two new files appeared in the components/ folder in the Explorer. Open components/PirateCard.tsx. At the top stands the agreement about props:
interface PirateCardProps {
name: string;
role: string;
avatar: string;
description?: string;
}Three fields are required, and the question mark in description? marks an optional field: the parent may leave it out. The interface does not change how the card works in the browser, because types disappear after compilation. It only makes sure the data matches the agreement.
Below the interface stands the component itself, exported by default:
export default function PirateCard({ name, role, avatar, description }: PirateCardProps) {
return (
<article className="rounded-lg border border-gray-300 p-4">
<div className="text-2xl font-bold">{avatar}</div>
<h3 className="mt-2 text-lg font-bold">{name}</h3>
<p className="text-gray-500">{role}</p>
{description && <p className="mt-2 text-sm">{description}</p>}
</article>
);
}The notation { name, role, avatar, description } pulls the props out of the object the function receives, and in JSX you put every value in curly braces. {description && ...} is conditional rendering: the paragraph appears only when a description exists. Be careful with numbers: {count && <span>New</span>} with count equal to 0 prints a zero on screen, so with numbers write count > 0 &&. export default lets other files import the card without curly braces.
What Claude wrote: Post
Open components/Post.tsx. The PostProps interface has four required fields, one of them numeric:
interface PostProps {
author: string;
avatar: string;
content: string;
likes: number;
}likes is of type number because likes are a number you will increase in lesson 2_3. Had Claude written string here, adding would turn into gluing text together.
The body of the component has three parts: a header with the avatar and the author, the content, and the like button:
export default function Post({ author, avatar, content, likes }: PostProps) {
return (
<article className="rounded-lg border border-gray-300 p-4">
<div className="flex items-center gap-3">
<div className="flex h-10 w-10 items-center justify-center rounded-full bg-gray-900 text-sm font-bold text-white">
{avatar}
</div>
<h3 className="font-bold">{author}</h3>
</div>
<p className="mt-3">{content}</p>
<button type="button" className="mt-3 text-sm text-gray-500">
Likes: {likes}
</button>
</article>
);
}rounded-full turns the square h-10 w-10 into a circle, and flex with items-center and justify-center puts the initials in the middle. The button does nothing yet: it only displays the number from props, because props are read-only. For the number to change on click, you will need the state from lesson 2_3.
Impressions of the seal on the feed and the profile
At the top of app/feed/page.tsx a line import Post from "@/components/Post"; appeared. @/ is the alias from lesson 1_2, the root folder of the project, so the import looks the same from every page. The list of posts now looks like this:
{posts.map((post) => (
<li key={post.id}>
<Post author={post.author} avatar={post.avatar} content={post.content} likes={post.likes} />
<Link href={`/post/${post.id}`} className="mt-1 inline-block text-sm text-blue-600 underline">
Open post
</Link>
</li>
))}You pass strings in quotes or in braces, and numbers always in braces: likes={post.likes} or likes={12}. The notation likes="12" would pass text, and the interface demands a number, so the build reports a type error. key sits on the element that map returns and comes from the stable id of the post. The index from map changes when a new post jumps to the top of the list, so it does not make a good key.
The post page uses the same Post under the <h1> heading. On the profile Claude did the same with the cards: it added a following array with three pirates, and under the <h2> heading "Pirates you follow" it maps it to <PirateCard key={pirate.id} ... />. Only Anne Bonny has a description, so on the other two cards you will check whether description && really leaves no empty paragraph. Data flows down: the page holds the data, passes it as props, and the component displays it.
Where the code runs: Server and Client Components
In the App Router, pages, layouts and components are Server Components by default. Next.js renders them on the server, and the browser gets the finished result without their JavaScript. In exchange they cannot use state, effects, event handling such as onClick, or browser APIs such as localStorage.
Client Components exist for that. The file starts with the "use client" directive, and Next.js sends its code to the browser, where React attaches clicks and state. The directive sets a boundary: everything such a file imports goes to the browser too.
I recommend one rule: Server Components by default, and "use client" only in small interactive files. In lesson 2_3 it will be the like button alone, not the whole post. You can check this without guessing. Ask Claude: "Which files in the project are Client Components and why?", then open project search in Cursor (the magnifier icon in the left bar) and type use client. After this lesson the result should be empty.
Captain's review
Before you commit, go through the diff in Source Control and check:
- One component per file.
components/Post.tsxexportsPost, andcomponents/PirateCard.tsxexportsPirateCard: the file name is the component name, so in a month you will find it with search. - Typed props.
likesis anumber, and onlydescriptionis optional. - Data from props. A typical agent mistake is a component with hard-coded data, for example
<h3>Anne Bonny</h3>instead of{author}. On the feed you will then see three posts with the same author, and in the diff a name inside the component file. - The limits of the request. No new file contains
"use client", andcomponents/crew.tsxdoes not appear on the list of changes.
At the end run npm run build, npm run lint and take a walk: /feed, /post/2 and /profile. The feed and the post page should look the same, because the same seal stamps them.
Composition in outline
You assemble components like building blocks: Post sits in a list item, the list on a page, and the page in the layout. How to build components that take any content inside, and how to pass all the fields of an object in one notation, lesson 2_4 will show. Here it is enough that you can order a component, read it and check its props.
Next step
Ask for a commit with a message like "Post and PirateCard components". Then fix the convention you have just checked in the ship's rules:
Add two lines to the "Crew messenger" section in @CLAUDE.md: components
in components/, one per file, the file name the same as the component
name, props described with an interface; "use client" only in small
files with state or events.Next time Claude will follow this convention without reminders, and you will check in the diff that it added only two lines. In lesson 2_3 the like button will finally start counting. In the editor below the same components wait in separate files: remove the description of one card or change the number of likes and see what changes.
Remember: there is one seal and many impressions, and the captain checks that nobody has carved someone else's name into it for good.
Code for this lesson: App.jsx
// The feed and the profile built from the components in the components/ folder.
// Change the data in the arrays below and see that the components stay the same.
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 },
{ id: '3', author: 'Mary Read', avatar: 'MR', content: 'Fish soup for dinner. First come, first served!', likes: 8 },
];
const following = [
{ id: '1', name: 'Anne Bonny', role: 'Captain', avatar: 'AB', description: 'The first to spot the kraken.' },
{ id: '2', name: 'Jack Rackham', role: 'Helmsman', avatar: 'JR' },
{ id: '3', name: 'Mary Read', role: 'Cook', avatar: 'MR' },
];
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}>
<Post
author={post.author}
avatar={post.avatar}
content={post.content}
likes={post.likes}
/>
</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-3">
{following.map((pirate) => (
<PirateCard
key={pirate.id}
name={pirate.name}
role={pirate.role}
avatar={pirate.avatar}
description={pirate.description}
/>
))}
</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. What are Props in React?
2. What does a React component return?
These are 2 of 8 questions for this lesson. Solve the rest in the game.
Hands-on tasks in the game
- Code editor
Write a PirateCard component (export default) with name, role and avatar props of type string. Describe the prop types in a TypeScript interface, e.g. PirateCardProps. The card shows the avatar, the name in an h3 heading and the role in a p paragraph.
- Horizontal ordering
Arrange the tokens that pass a name prop to the component:
- Click in order
Click the tokens to use the Post component with no props:
- Vertical ordering
Arrange the lines of a simple component definition:
- Code editor
Write a Twitter-style Post component (export default): describe the props author and content of type string and likes of type number in a TypeScript interface. Show the author in an h3 heading, the content in a p paragraph and the number of likes in a button. Style the post with Tailwind classes in className, e.g. bg-white p-6 rounded-lg shadow.
- Horizontal ordering
Arrange the tokens that start mapping over the pirates array:
- Vertical ordering
Arrange the lines that render a list of pirates with map():
- Click in order
Click the tokens to import the Post component through the @/ alias:
- Vertical ordering
Arrange how data reaches a component through props:
Tasks after this lesson
- QuestionQuiz: Props
- QuestionWhat a component returns
- QuestionTyping props with an interface
- Coding taskPractice: Pirate card component
- Put in orderPassing a string prop
- QuestionAn optional prop
- QuestionQuiz: Client vs Server Components
- QuestionThe default component type
- Click in orderUse a component in JSX
- Put in orderStructure of a component definition
- Coding taskPractice: Post component
- QuestionThe key prop with map()
- Put in orderCalling .map() on a list
- Put in orderStructure of rendering a list
- QuestionConditional rendering
- Click in orderImport of the Post component
- Put in orderData flow through props