Sub topic 3
Passing callbacks down so children can request changes
This chapter assumes you know JavaScript arrow functions and destructuring, how to write function components in JSX, how to pass props, how useState works, and how to handle events like onChange. It covers one idea: how a child component asks its parent to change state it does not own.
The rule: data flows down, requests flow up
In React, props move in one direction, from parent to child. A child can read a prop, but it must not change it. Props are read-only.
That raises a question. If a parent owns a piece of state and a child holds the button or input the user touches, how does the child cause the state to change?
The child cannot write to the parent's state. Instead, the parent passes down a function. The child calls that function when something happens, and the parent runs it. Because the function was defined in the parent, it can call the parent's state setter.
A function passed as a prop is usually called a callback. By convention, callback props start with on, such as onChange or onShow. The handler written in the parent often starts with handle.
A small example
Here is the pattern from the second code example in this tutorial, reduced to one child.
import { useState } from 'react';
function Field({ label, value, onChange }) {
return (
<label>
{label}: <input value={value} onChange={e => onChange(e.target.value)} />
</label>
);
}
export default function App() {
const [text, setText] = useState('');
return <Field label="First" value={text} onChange={setText} />;
}Follow what happens when the user types a:
- The browser fires the input's
onChangeevent. - The arrow function in
FieldcallsonChange('a'). onChangeis the same function assetText, becauseApppassed it down.- React updates
textinAppand rendersAppagain. Fieldreceivesvalue="a"and shows it.
Field never stores the text. It only displays what it was given and reports what the user did.
What the child sends back
The child decides what information to send. The parent decides what to do with it. This split keeps both sides simple.
Fieldsends the new text, because that is the useful part of the event. It does not send the whole event object.- In the accordion example,
PanelcallsonShow()with no arguments. The parent already knows which panel it handed that callback to, so it sets the right index.
Compare two ways to pass the callback in the accordion:
<Panel
title="Details"
isActive={activeIndex === 1}
onShow={() => setActiveIndex(1)}
>
More details here.
</Panel>Here the parent wraps the setter in an arrow function that already contains the index. Panel does not need to know it is panel number 1.
Pass the function, don't call it
The most common mistake is calling the function while writing the JSX.
// Wrong: runs during render, and passes its result (undefined) as the prop
<Panel onShow={setActiveIndex(1)} />
// Right: passes a function that React can call later
<Panel onShow={() => setActiveIndex(1)} />The wrong version calls setActiveIndex(1) every time App renders. That triggers another render, which calls it again, and React reports too many re-renders.
The same rule applies inside the child. Write onClick={onShow} or onClick={() => onShow()}, not onClick={onShow()}.
Common errors
| Symptom | Likely cause |
|---|---|
| Too many re-renders | A callback was called in JSX instead of passed |
| `onChange is not a function` | The parent forgot to pass the prop, or used a different name |
| Typing does nothing | The child shows `value` but the callback never updates the parent's state |
| Parent receives an event object, not text | The child passed `e` instead of `e.target.value` |
Why this matters for lifting state
Lifting state up needs both halves of this pattern. The value flows down as a prop. The callback carries requests up. Together they let several children read and change one value that lives in their parent. The next chapters use this to fix two components that hold separate copies of the same data.
Exercise
A parent has const [count, setCount] = useState(0); and renders <Counter onIncrement={setCount(count + 1)} />. The app fails with "Too many re-renders". What is wrong, and how do you fix it?
Show answer
setCount(count + 1) is called during render instead of being passed as a function. Each call triggers another render, which calls it again. Pass a function instead: onIncrement={() => setCount(count + 1)}. Inside Counter, call it with onClick={onIncrement}.
Exercise
In the Field example, Field calls onChange(e.target.value). Why is it better to send the text than to send the whole event object e?
Show answer
The parent only needs the new text. Sending the text keeps the parent independent of the DOM event, so Field can later be changed (for example to a custom input) without changing the parent.