Pass data into components with props and compose UIs from smaller pieces.
Props (short for properties) are how you pass data from a parent component to a child. They make components reusable.
function Greeting({ name }: { name: string }) {
return <h1>Hello, {name}!</h1>;
}
// Using it:
<Greeting name="Alice" />
<Greeting name="Bob" />
For larger components, define a props interface:
interface CardProps {
title: string;
body: string;
highlighted?: boolean;
}
function Card({ title, body, highlighted = false }: CardProps) {
return (
<div style={{ fontWeight: highlighted ? "bold" : "normal" }}>
<h2>{title}</h2>
<p>{body}</p>
</div>
);
}
children propAny JSX nested inside a component tag is available as children:
function Box({ children }: { children: React.ReactNode }) {
return <div style={{ border: "1px solid #ccc", padding: "1rem" }}>{children}</div>;
}
<Box>
<p>I'm inside the box!</p>
</Box>
Build complex UIs from small, single-purpose components:
function App() {
return (
<Box>
<Card title="TypeScript" body="A typed superset of JavaScript" highlighted />
<Card title="React" body="A library for building UIs" />
</Box>
);
}
Create a UserCard component that accepts:
name: stringrole: stringavatar?: string (optional — show initials if missing)And render it twice in App with different props. Each card should display the name and role in the DOM.
export default function App(): JSX.Element { return <h1>Hello world</h1> }
No test results yet
Click Run Tests or press Ctrl+Enter