Courses / React 19: From Components to Production / Chapter 4
Module 2 · React Foundations · Chapter 4 of 30
Components: Props, Children, Conditions, Lists and Keys
Builds on Introduction to React: Elements, JSX and the First Render and JavaScript for React: Values, Closures, Arrays, Async and the DOM. Next up: State and Effects: useState, Events, useEffect and Cleanup.
Part 1 · Function components and props
A component is a function that returns UI
A kirana shop's product catalogue is a screen made of the same few shapes repeated: a header, a row of category buttons, many product cards. React lets you write each shape once and use it as many times as you need.
A component is a JavaScript function that returns what to show, written in JSX. Its name starts with a capital letter. You use it like a tag, so <ProductCard /> calls ProductCard whenever React renders that place. Each component should have one job, such as showing a card or a list of cards.
Data goes in through props, short for properties. Props are the attributes you write on the tag, collected into one object that React passes as the function's first argument. A tag with name="Toor dal" and price={168} gives the function { name: "Toor dal", price: 168 }. Props flow one way, from parent to child.
Props are read-only. A component must not change its props or the objects inside them. If a value needs to change over time, it belongs to state, which the next chapter covers.
Most code destructures the props right in the parameter list, function ProductCard({ name, price }: ProductCardProps), so the body reads the names directly. When an object already has the right shape, <ProductCard {...product} /> passes every field as a prop. Use that spread sparingly: it hides which props a card really receives, and it passes along fields the card never asked for.