Skip to content
ClapelloNotes by Barath
ReactCh 04 · 2/6
Core4 min readUpdated

React / 17

children, read-only props & one-way data flow

TL;DR

Whatever you nest inside a component arrives as props.children. Props only flow down, a child never edits them, and it asks the parent for changes through a callback.

children: what goes between the tags

When you nest content inside a component, React passes it in a prop called children.

Card.jsx
function Card({ title, children }) {
  return (
    <section className="card">
      <h2>{title}</h2>
      {children}
    </section>
  );
}
 
function App() {
  return (
    <Card title="Today's special">
      <p>Masala dosa with two chutneys.</p>
      <button>Add to cart</button>
    </Card>
  );
}

Card doesn't know or care what's inside. It draws the frame and puts {children} where the content belongs. children can be text, one element, several elements, or undefined if nothing was nested.

It's the same third argument of createElement from Chapter 1. JSX just lets you write it between the tags.

Props are read-only

A component must never change its own props:

Don't

Editing props

function Price(props) {
  props.amount = props.amount * 1.18; // ❌ TypeError in development
  return <p>₹{props.amount}</p>;
}

Do

Compute a new value

function Price({ amount }) {
  const withTax = amount * 1.18;
  return <p>₹{withTax.toFixed(2)}</p>;
}

React expects components to be pure: the same props always give the same output, and rendering changes nothing outside the component. If a child could edit its props, the parent's data and the screen would quietly disagree. In development React freezes the props object, so the bad version throws right away.

One-way data flow

Data in React flows one way: down. The component that owns some data passes it to its children as props. If a child needs that data to change, it doesn't edit it; it asks, by calling a function the parent passed down.

parent · owns the data

App

count = 2

props go down

count, onAdd

requests go up

onAdd()

child · reads, never writes

AddButton

shows count, calls onAdd on click
Data flows one way: down, as props. A child that wants a change calls a function the parent gave it. The parent changes its own data, and new props flow down.
App.jsx
import { useState } from "react";
 
function AddButton({ count, onAdd }) {
  return <button onClick={onAdd}>In cart: {count}</button>;
}
 
export default function App() {
  const [count, setCount] = useState(0);
  return <AddButton count={count} onAdd={() => setCount(count + 1)} />;
}

useState gets its own chapter next. For now, read it as "App owns count and has a way to change it". The button only shows count and calls onAdd. App changes the number, re-renders, and passes the new count down.

By convention, callback props start with on (onAdd, onSelect), and the functions that handle them start with handle (handleAdd).

Gotchas

Recall

Can you answer these?

0/4

Answer out loud (or in your head) before you tap. That's what makes it stick.

Saved on this device only. No login.