Basic component
Props
Define aComponentNameProps interface with on prefix for callbacks. Destructure props in the function parameter:
Children
Use thechildren helper to access child content passed via JSX:
Conditional rendering
Use&& for show/hide and ternary for either/or:
task.status changes, only the affected branch updates. No re-render of the entire component.
Lists
Use.map() with a key prop:
key prop tells the runtime how to efficiently update the list when items are added, removed, or reordered.
Events
Attach event handlers directly in JSX:Composition
Compose components with JSX — never call them as functions.Refs
Access the underlying DOM element withref():
Lifecycle
Components run once, but you can hook into mount withonMount():
onMount runs after the component’s DOM is inserted. The optional cleanup function runs when the component is removed.
Compound components
Compound components are groups of related components designed to work together — likeCard with CardHeader, CardContent, and CardFooter, or Dialog with Dialog.Header, Dialog.Title, and Dialog.Footer.
Render children in order
Compound components should render children in the order the developer provides them. Don’t inspect or reorganize children into “correct” slots:CardFooter before CardContent, they’ll see the footer first — that’s their responsibility, not the component’s. Reordering children adds complexity and can cause issues with hydration, where the client-side DOM structure must match what the server rendered.