Module 3.3: Lifting State Up - The State Sharing Technique

π― Lesson Goals
After this lesson, you will master:
- The Data Sharing Problem: Identify scenarios where two or more sibling
componentsneed to use the samestate. - The Lifting State Up Technique: Understand the process of moving
statefrom a childcomponentup to itsclosest common ancestorcomponent. - Single Source of Truth: Grasp the principle of maintaining a single, reliable source of data in your application.
1. The Problem: State Only Exists Locally
In React, useState creates private state for that specific component. Other components (even sibling components right next to it) cannot access this state.
The Scenario:
Imagine you have two child components:
InputSection: For entering a name.DisplaySection: For displaying a greeting with the entered name.
If you declare the name state inside InputSection, DisplaySection wonβt be able to know what name is to display it.
// β Problem: DisplaySection cannot access InputSection's 'name'
function Parent() {
return (
<>
<InputSection />
<DisplaySection />
</>
);
}2. The Solution: Lifting State Up
To share state between components, we must move that state up to their closest common ancestor component.

The 3-Step Process
- Remove the
statefrom the childcomponents. - Move that
stateup to the common parentcomponent. - Pass down the
state(data) and thesetter function(toupdateit) to the children viaprops.
Example Code after Lifting State:
function Parent() {
// 1. & 2. State lives in the Parent
const [name, setName] = useState('');
return (
<>
{/* 3. Pass down state and setter */}
<InputSection name={name} onNameChange={setName} />
<DisplaySection name={name} />
</>
);
}3. Hands-On Labs
3.1. Lab 1: FAQ Accordion (Exclusive Open)
Build a Frequently Asked Questions (FAQ) list.
- Requirements: At any given time, a MAXIMUM of only one answer can be open. Opening one automatically closes another.
- UI: Add a rotation effect to the arrow icon when opening/closing.

π‘ Hint:
- The
activeIndexstate(which stores theindexof the currently open panel) must reside in the parentcomponent(Accordion).- The parent
passes downisActive(a boolean) andonToggle(afunction) to eachAccordionItem.- The child simply calls
onToggle()when clicked; the parent retains thedecision-making powerfor opening/closing.
3.2. Lab 2: Live Currency Converter (Bidirectional)
Build a real-time currency conversion tool.
- Two
input fields: VND and USD. - Enter VND -> Automatically calculates USD.
- Enter USD -> Automatically calculates VND.
- Add a βResetβ button to clear both
fields.

π‘ Hint:
- Avoid using two separate
states(vnd,usd) as this can easily lead to beingout of sync.- Instead, use a model with
amount(the monetary value) andscale(theunit, βvβ for VND or βuβ for USD).- The parent
componentcalculates the display values for bothinput fieldsbased on the currentamountandscale.
4. Summary
- Lifting State Up is a
techniquefor movingstateto a common parentcomponenttoshare dataamong childcomponents. - The parent
componentbecomes the βSource of Truthβ. - Child
componentsbecome βControlled Componentsβ (controlled bypropsfrom the parent). - Always prioritize keeping
stateas low as possible; onlylift upwhen you truly need toshareit.
5. References
Last updated on