Pass read-only props
Give a component its inputs
Props are inputs supplied to a component by its parent. They let a single component describe different content without changing its definition. In the Chapter 3 client project, replace src/App.jsx with each complete JSX example in this chapter after its dependencies are approved. Expected browser output is described, but has not been observed in a React runtime.
function ProductCard({ name, price, available }) { return ( <article> <h2>{name}</h2> <p>{price} credits</p> <p>{available ? 'In stock' : 'Unavailable'}</p> </article> );}
export default function App() { return ( <main> <h1>Art supplies</h1> <ProductCard name="Sketchbook" price={12} available /> <ProductCard name="Ink" price={8} available={false} /> </main> );}Expected: two articles, Sketchbook (12 credits, In stock) and Ink (8 credits, Unavailable). Quoted JSX attributes pass strings; braces evaluate JavaScript expressions. A boolean prop written without a value is true. The ternary chooses one of two strings; conditional rendering is covered in Chapter 7. The component receives an object of props. Destructuring in the parameter reads three fields, equivalent to reading props.name, props.price and props.available.
Pass objects and arrays without changing them
function SupplySummary({ product, tags }) { const sortedTags = [...tags].sort(); return ( <article> <h2>{product.name}</h2> <p>{product.quantity} remaining</p> <p>{sortedTags.join(', ')}</p> <p>Original order: {tags.join(', ')}</p> </article> );}
export default function App() { return <main><SupplySummary product={{ name: 'Paper', quantity: 4 }} tags={['Recycled', 'A4']} /></main>;}Expected: Paper, 4 remaining, A4, Recycled, and Original order: Recycled, A4. The child can read the original array but must not sort it in place. The spread expression copies its elements into a new array before sorting. The copy is shallow; nested objects are still shared. Read-only here describes a React programming rule, not a guarantee that every input object is frozen by JavaScript.
Send a callback instead of mutating an input
function RequestButton({ name, onRequest }) { return <button type="button" onClick={() => onRequest(name)}> Request {name} </button>;}
export default function App() { function recordRequest(name) { console.log('Requested: ' + name); } return <main><RequestButton name="Brush" onRequest={recordRequest} /></main>;}Expected: a keyboard-accessible button labeled Request Brush. Activating it logs Requested: Brush once per handler invocation in the developer console; the page text does not change. The parent supplies a function and the child calls it in response to an event. Do not call onRequest during rendering. Chapter 9 teaches event handlers and Chapter 10 teaches state updates. When data changes, the parent can provide a fresh value rather than asking the child to modify a prop.
Special inputs and mistakes
Avoid assigning product.quantity = 0 or changing the array received from the parent. Props describe the current render; later renders may receive different inputs. React uses key for identity rather than passing it as a regular component input. Pass a separate id prop if the child needs the identifier. React 19 introduced ref as a prop for function components; React 18 usually uses forwardRef for that purpose. Refs and keys have dedicated later lessons. Do not assume either is an ordinary application field across versions.
Practice
Create a reusable stock label for two fictional products. Pass an object and an array, and derive a sorted label without changing the array. Explain which inputs the parent owns.