React Props and Composition: Data Down, Calls Up, Children in the Slot
Follow props from a parent's tag into a component and onto the page: one props object, destructuring, an interface with an optional prop and a default, the read-only rule, a callback that goes back up, and the children slot that lets one Card hold anything.
What you will learn
- Read props as the one object React passes to a component
- Type props with an interface, optional props and default values
- Pass a function down so a child can ask its parent for a change
- Build a page from small pieces with the children slot
Before you start
- React components and JSX (see Why React, JSX and Components)
- TypeScript basics: types and interfaces
A component is a function, and props are its arguments. A parent writes a tag with attributes, React gathers them into one object, and calls the component with it. That one idea explains almost everything in this article: how data reaches a component, why a child can't change it, and how a big page gets built from small pieces.
The examples come from a small task app with a TaskCard, a Card box, a Board and a DoneButton. The outputs are from the lecture's own demo scripts.
Attributes become one props object
// Example 1: a component and its props
interface TaskCardProps {
title: string
owner: string
done?: boolean // optional: it has a default
}
// React hands ALL the attributes over as one object
export function TaskCard(props: TaskCardProps) {
console.log('TaskCard got', props)
const { title, owner, done = false } = props
return (
<div className="card">
<h3>{title}</h3>
<p>Owner: {owner}</p>
<p>{done ? 'Done' : 'To do'}</p>
</div>
)
}
A demo renders <TaskCard title="Write report" owner="Emma" done={true} />. Notice the two kinds of value. "Write report" in quotes is a string. {true} in curly braces is a real JavaScript boolean, not the text "true". Line 10 logs what the function receives:
TaskCard got { title: 'Write report', owner: 'Emma', done: true }
page: Write report | Owner: Emma | Done
Three attributes, one object, passed as the single argument.
Destructuring: one object, three names
Writing props.title everywhere gets long, so line 11 destructures the object: const { title, owner, done = false } = props. Each key gets its own name, and the JSX drops those values into the page. They are the same values as props.title and props.owner, with shorter names.
An interface describes the shape
Lines 2 to 6 are interface TaskCardProps. Think of it as a printed form with three blanks: title is a string, owner is a string, done is a boolean. The question mark after done means a tag may leave it out. TypeScript checks every TaskCard tag in the project against this shape. Here are two wrong tags:
// What tsc says when a tag does not fit the props.
import { TaskCard } from '../src/TaskCard'
// owner is left out
export const a = <TaskCard title="Pay invoice" />
// done gets a string, not a boolean
export const b = (
<TaskCard title="Pay invoice" owner="Olivia"
done="yes" />
)
scripts/break-types.tsx(5,19): error TS2741: Property 'owner' is missing in type '{ title: string; }' but required in type 'TaskCardProps'.
scripts/break-types.tsx(10,5): error TS2322: Type 'string' is not assignable to type 'boolean | undefined'.
A missing prop and a wrong type both fail before any code runs, and the compiler tells you which prop and which line.
One component, different props
Here is where props pay off. App uses TaskCard three times inside a Card:
// Composition: the page built from small components
import { Board } from './Board'
import { Card } from './Card'
import { TaskCard } from './TaskCard'
export default function App() {
return (
<>
<Card heading="This week">
<TaskCard title="Write report" owner="Emma"
done={true} />
<TaskCard title="Book flights" owner="Jack"
done={false} />
<TaskCard title="Pay invoice"
owner="Olivia" />
</Card>
<Card heading="Notes">
<p>Stand-up is at ten.</p>
</Card>
<Card heading="Today">
<Board />
</Card>
</>
)
}
React builds a different props object for each tag and calls the same function once per tag. The fourth line comes from the Board further down the page:
TaskCard got { title: 'Write report', owner: 'Emma', done: true }
TaskCard got { title: 'Book flights', owner: 'Jack', done: false }
TaskCard got { title: 'Pay invoice', owner: 'Olivia' }
TaskCard got { title: 'Call Jack', owner: 'Emma', done: false }
card: Write report - Done
card: Book flights - To do
card: Pay invoice - To do
One design, three cards, no copied markup. Change TaskCard once and all of them change.
A default value for a missing prop
Look at the third line of that output. Pay invoice has no done attribute, so its props object simply has no done key. Without help, done would be undefined. The default in the destructuring, done = false, fills the gap, so the card says To do. The default is used only when the prop is missing. When the parent passes done, the parent wins.
Props flow one way: down
Props ride along the branches of the tree, always from parent to child. The other direction doesn't work: a TaskCard can't reach up and change what App gave it.
What if a child changes its props?
// What happens when a child changes its own props.
import { show } from './screen'
function Rename(props: { title: string }) {
props.title = 'Changed' // not allowed
return <h3>{props.title}</h3>
}
try {
await show(<Rename title="Write report" />)
} catch (e) {
const err = e as Error
console.log(`${err.name}: ${err.message}`)
}
TypeError: Cannot assign to read only property 'title' of object '#<Object>'
In development React freezes the props object, so the assignment on line 5 throws. The parent's title stays Write report. Props are read only.
A function as a prop: the callback goes up
So how does anything change? The parent owns the data and passes down a function to change it. Here is Board:
// Example 3: the parent passes a function down
import { useState } from 'react'
import { DoneButton } from './DoneButton'
import { TaskCard } from './TaskCard'
export function Board() {
// state: data the parent owns, kept by React
const [done, setDone] = useState(false)
function handleDone(title: string) {
console.log('Board heard:', title)
setDone(true)
}
return (
<div>
<TaskCard title="Call Jack" owner="Emma"
done={done} />
<DoneButton title="Call Jack"
onDone={handleDone} />
</div>
)
}
Line 8 keeps done in state, data the parent owns and React keeps between renders (state gets its own article next). handleDone is a function, and a function is a value like any other, so line 20 hands it down as onDone. The child:
// Example 3: the child gets a function as a prop
interface DoneButtonProps {
title: string
// a function the parent hands down
onDone: (title: string) => void
}
export function DoneButton(props: DoneButtonProps) {
const { title, onDone } = props
// the child cannot change the data, so it calls up
return (
<button onClick={() => onDone(title)}>
Mark done
</button>
)
}
Line 5 types onDone as a function that takes a string and returns nothing. On a click, line 13 calls it with the title, and that call runs Board's handleDone. Here is one click, followed in the demo:
TaskCard got { title: 'Call Jack', owner: 'Emma', done: false }
page: Call Jack | Owner: Emma | To do
click: Mark done
Board heard: Call Jack
TaskCard got { title: 'Call Jack', owner: 'Emma', done: true }
page: Call Jack | Owner: Emma | Done
Board sets done to true, React renders Board again, and new props come down to TaskCard. The child never touched the data. It asked, and the parent made the change.
children: a slot the parent fills
// Example 4: children - a slot the parent fills
import type { ReactNode } from 'react'
interface CardProps {
heading: string
children: ReactNode // what sits between the tags
}
export function Card(props: CardProps) {
const { heading, children } = props
return (
<section className="box">
<h2>{heading}</h2>
{children}
</section>
)
}
Whatever sits between <Card> and </Card> arrives in props as children, typed as ReactNode (anything React can draw), and lands where line 15 puts it. In App, one Card holds three task cards, one holds a paragraph, and one holds the Board. The demo prints what landed in each slot:
Card "This week" children: div, div, div
Card "Notes" children: p
Card "Today" children: div
One Card, three different insides. This is composition: small pieces that don't need to know what they will hold.
Common mistakes
- Writing
done="true". That is the string "true". Usedone={true}for a boolean. - Changing
props.somethinginside a child. Ask the parent with a function prop instead. - Calling the callback while rendering:
onClick={onDone(title)}runs immediately. Pass a function:onClick={() => onDone(title)}. - Forgetting to render
{children}, so whatever the parent put between the tags never shows.
Recap
| Idea | What to remember |
|---|---|
| Props | The tag's attributes, gathered into one object, passed as the one argument |
| Interface | Names every prop and its type; ? marks an optional prop |
| Default | done = false in the destructuring fills a missing prop |
| Direction | Props go down and are read only |
| Function prop | The child calls it to ask the parent for a change |
| children | Everything between the tags, placed where {children} sits |
Try it yourself: add a fourth TaskCard with a new owner, then give Card an optional footer prop and render it under the children. The next article, state, events, lists and conditional rendering, looks at the useState line in Board properly.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
The Python Architect Masterclass: Full Stack to AI Engineer
Python, Django, FastAPI, React, Docker, AWS, LLMs, RAG, agents and MCP, from first steps to AI engineer.View on Udemy ↗: The Python Architect Masterclass: Full Stack to AI Engineer (paid course, opens in a new tab)Related posts
One Request, Two Answers: the Same Post as an HTML Page and as JSON
Ask a Django blog for the same post twice: once as a 12,731-byte HTML page built by a template, once as 2,976 bytes of JSON built by a serializer. Compare the headers, see who builds the page, and why the server still owns every write.
A Blog Design from a Prompt: Downloading Figma Make Code and Reading App.tsx and BlogCard.tsx
Make a blog design from a prompt in Figma Make, download src.zip, unzip it with Python's zipfile module, and read the two files that matter: App.tsx with its selectedPost state and BlogCard.tsx with its props.
How a Web Page Works: HTML Tags, Elements and Attributes
A web page is a text file full of labelled pieces. Learn tags, attributes and nesting with three tiny pages, and see what breaks when an attribute or a closing tag is wrong.
Semantic HTML: header, nav, main, aside and footer as Page Landmarks
The same page written as div soup and with semantic tags looks identical, but only one gives screen readers and search engines landmarks and an outline. Plus tables, div, span and the one-main rule.
