ReactEasy
What are props?
Props (properties) are the inputs a parent passes to a child component, written like HTML attributes. They are read-only in the child: a component must never modify its props, only render from them or call callback props to ask the parent for changes.
What can be a prop
Strings, numbers, booleans, objects, arrays, functions (callbacks), JSX elements, and children (the content between the tags). Since React 19, ref is also a regular prop for function components.
Patterns
- Destructuring with defaults:
function Button({ size = "md", children }). Default values in destructuring replacedefaultPropsfor function components (removed in React 19). - Callbacks up:
onChange,onSelectlet children tell parents something happened. - Spreading:
<input {...rest} />forwards remaining props; use with care. - Typing: TypeScript interfaces or types document and check props.
Rules
- Props flow down only (one-way data flow).
- Treat props as immutable. To change what is displayed, the parent updates its state and passes new props.
- When a parent re-renders, children receive new props and re-render too (unless memoized).
Props vs state
Props are configured by the parent; state is owned and changed by the component itself.
Code example
tsx
1type AlertProps = {2 tone?: "info" | "error";3 title: string;4 onDismiss?: () => void;5 children?: React.ReactNode;6};78export function Alert({ tone = "info", title, onDismiss, children }: AlertProps) {9 // props.title = "x"; // never mutate props10 return (11 <div role="alert" className={tone === "error" ? "bg-red-100" : "bg-blue-100"}>12 <strong>{title}</strong>13 <div>{children}</div>14 {onDismiss && <button onClick={onDismiss}>Dismiss</button>}15 </div>16 );17}1819export function Page() {20 return (21 <Alert tone="error" title="Payment failed" onDismiss={() => console.log("closed")}>22 Your card was declined. Try another card.23 </Alert>24 );25}
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.