Components and props
A component is a JavaScript function that returns JSX, and props are its inputs passed from the parent like HTML attributes. Together they replace copy-pasted markup with one reusable piece of UI that you call with different data.
25.1 The problem: copy-pasted cards
- App has two product cards with exactly the same structure; only name and price differ.
- It renders fine, but with 50 products a design change (say, a rating line) means editing 50 places.
- Forget one copy and the UI becomes inconsistent.
React (JSX)
export default function App() { return ( <main> <div className="card"> <h3>iPhone 16</h3> <p>₹79900</p> </div> <div className="card"> <h3>Pixel 9</h3> <p>₹74999</p> </div> </main> );}Output
# iPhone 16₹79900# Pixel 9₹7499925.2 Why components?
- The JavaScript answer to repetition is a function: write once, call with different values.
- React uses the same idea: a function that builds UI is a component, its arguments are props.
- Step 1
1.Copy paste: same card code 50 times
- Step 2
2.One design change: edit 50 places
- Step 3
3.Extract a function: ProductCard
- Step 4
4.Call it 50 times with different props
25.3 Component: just a function
- ProductCard is a normal function returning JSX.
- Its name starts with a capital letter; React treats lowercase tags as HTML tags.
- { name, price } destructures the props object.
- export default lets other files import it.
React (JSX)
function ProductCard({ name, price }) { return ( <div className="card"> <h3>{name}</h3> <p>₹{price}</p> </div> );} export default ProductCard;25.4 Passing props
- Use the component like an HTML tag and pass props like attributes.
- Strings go in quotes; numbers (and any other JS value) go in curly braces: price={79900}.
- React collects the attributes into one object and passes it to ProductCard.
- The same component is reused for the Pixel card.
React (JSX)
import ProductCard from "./ProductCard"; export default function App() { return ( <main> <ProductCard name="iPhone 16" price={79900} /> <ProductCard name="Pixel 9" price={74999} /> </main> );}Output
# iPhone 16₹79900# Pixel 9₹7499925.5 How props flow
- Step 1
1.App (parent) writes name and price
- Step 2
2.React packs them into one props object { name, price }
- Step 3
3.ProductCard (child) receives it as the function argument
- Step 4
4.ProductCard returns the UI: the card on screen
Data always goes down from parent to child
25.6 The children prop: composition
- Sometimes a component needs a whole piece of JSX, not just data.
- Whatever you write between <Card> and </Card> arrives automatically in props.children.
- Card provides the border and padding; the user of Card decides what goes inside.
- Combining small components into a bigger UI is called composition.
React (JSX)
function Card({ children }) { return ( <section className="card"> {children} </section> );}export default function Checkout() { return ( <Card> <h2>Payment</h2> <button>Pay ₹499</button> </Card> );}Output
# Payment[button] Pay ₹49925.7 Typing props with TypeScript
- Define a props type: name: string, price: number, tag?: string (optional).
- tag = "New" in the destructuring is the default when the parent omits it.
- Passing price="74999" (a string) is a compile error before the code runs.
React (TSX)
type ProductCardProps = { name: string; price: number; tag?: string;}; function ProductCard({ name, price, tag = "New",}: ProductCardProps) { return <p>{name} ₹{price} {tag}</p>;} <ProductCard name="Pixel 9" price="74999" />;Output
$ npx tsc --noEmitProductCard.tsx:15:29 - error TS2322:Type 'string' is not assignableto type 'number'.Checkpoint: price="500" is a string, so "500" + 50 concatenates to 50050. Pass price={500} to get 550.
React (JSX)
function Total({ price }) { return <p>Total: {price + 50}</p>;} export default function App() { return <Total price="500" />;}Output
Total: 5005025.8 Common mistakes
- Changing props inside the child (props.price = ...): props are read-only and React throws a TypeError in development. Compute a new variable instead.
- Passing numbers in quotes: they arrive as strings.
- Lowercase component names: React renders them as unknown HTML tags.
- Thinking props can only be text or numbers: arrays, objects, functions and JSX all work; children is just a normal prop.
React (JSX)
// Wrong: TypeError, props are read-onlyfunction Price(props) { props.price = props.price * 0.9; return <p>Sale: ₹{props.price}</p>;} export default function App() { return <Price price={1000} />;}React (JSX)
// Right: Sale: ₹900function Price({ price }) { const salePrice = price * 0.9; return <p>Sale: ₹{salePrice}</p>;} export default function App() { return <Price price={1000} />;}| Prop value | Syntax | Example |
|---|---|---|
| String | Quotes | name="iPhone 16" |
| Number | Braces | price={79900} |
| Array / object | Braces | tags={["new", "sale"]} |
| Function | Braces | onAdd={addItem} |
| JSX | Between the tags | <Card><h2>Payment</h2></Card> |
25.9 Four rules for components
- Component names start with a capital letter.
- Props are read-only: the same props should always give the same UI.
- One component, one job; split big files into smaller components.
- Give defaults in the destructuring (defaultProps for function components was removed in React 19).
25.10 Interview tips
- Props are the inputs of a component, passed from parent to child like function arguments.
- React passes them as one object; data flows one way, parent to child.
- Props are read-only because a component should behave like a pure function.
- children enables composition (wrapper components like Card, Layout, Modal).
- A function passed as a prop lets a child tell the parent something happened.