Have you ever wondered how a child component in React can send data or trigger actions in its parent? ๐ค
That exact concept โ child-to-parent communication โ is one of the most important patterns every React developer needs to understand.
Today, Iโll walk you through how I built a small app called โListo ๐โ, where users can add, delete, and toggle packed items, all powered by Reactโs state and props magic โจ
โ๏ธ Step 1: The State Lives in the Parent
In React, state should live where itโs needed the most.
Since multiple child components (Form, PackingList, Stats) need to access the same list of items, I kept the state in the parent App component.
function App() {
const [items, setItems] = useState([]);
}
Now App becomes the single source of truth for the data.
๐งพ Step 2: Adding an Item (Child โ Parent)
To add a new item, I created a function in the parent that updates the state:
function handleAddItem(item) {
setItems((items) => [...items, item]);
}
Then I passed this function down to the Form component as a prop:
<Form onAddItem={handleAddItem} />
Inside the form, I created the new item and called the parent function:
function handleSubmit(e) {
e.preventDefault();
if (!description) return;
const newItem = {
description,
quantity,
unit,
packed: false,
id: Date.now(),
};
onAddItem(newItem);
}
๐ก Concept:
When the form submits, it doesnโt change state itself โ it calls a function from the parent, sending data upward. Thatโs child-to-parent communication in action!
๐๏ธ Step 3: Deleting an Item
For delete, the logic again stays in the parent:
function handleDeleteItem(id) {
setItems((items) => items.filter((item) => item.id !== id));
}
And we pass it down to each Item component through props:
<button onClick={() => onDeleteItem(item.id)}>โ</button>
When the button is clicked, the child triggers the parentโs delete logic โ clean and simple.
โ Step 4: Toggling โPackedโ Status
This part flips the packed value between true and false using the itemโs id.
function handleTogglePacked(id) {
setItems((items) =>
items.map((item) =>
item.id === id ? { ...item, packed: !item.packed } : item
)
);
}
Then inside each Item:
<input
type="checkbox"
checked={item.packed}
onChange={() => onTogglePacked(item.id)}
/>
Now, clicking the checkbox updates the parent state instantly.
โ
No complex logic, just pure React reactivity.
๐ง What I Learned
This small app might look simple, but it teaches some of the core principles of React:
- Keep state in the parent where multiple children can access it.
- Pass functions as props to let children communicate back.
- Always update state immutably (create new arrays/objects).
Once you understand this flow, everything in React โ from forms to dashboards โ becomes easier to reason about.
โจ Final Thoughts
Building โListo ๐โ helped me finally connect theory with practice.
I used concepts like:
โ
Lifting state up
โ
Child-to-parent communication
โ
Controlled components
If youโre learning React, try this pattern in your own projects โ itโll solidify your understanding of how data flows in React. ๐ช
Top comments (0)