📖 Scenario: You are building a simple React app where two sibling components need to share and update the same data. This is a common situation where lifting state up helps keep data consistent.
🎯 Goal: Build two sibling components that share a counter value. The counter state should live in their common parent. One sibling increments the counter, and the other displays the current count.
📋 What You'll Learn
Create a parent component called CounterParent with a state variable count initialized to 0
Create a child component called Incrementer that has a button to increase count by 1
Create a child component called DisplayCount that shows the current count value
Lift the count state up to CounterParent and pass necessary props to children
💡 Why This Matters
🌍 Real World
Many React apps need multiple components to share and update the same data. Lifting state up is a simple and common pattern to keep data consistent and synchronized.
💼 Career
Understanding lifting state is essential for React developers to build maintainable and scalable user interfaces where components communicate effectively.
Progress0 / 4 steps
1
Create the parent component with initial state
Create a React functional component called CounterParent. Inside it, use useState to create a state variable called count initialized to 0. Return a div with the text Parent Component.
React
Hint
Use const [count, setCount] = useState(0); inside the CounterParent function.
2
Add child components Incrementer and DisplayCount
Inside the same file, create two new functional components: Incrementer and DisplayCount. For now, have Incrementer return a button with text Increment. Have DisplayCount return a div with text Count: 0. Update CounterParent to render both components inside a div.
React
Hint
Define two new functions Incrementer and DisplayCount that return the specified JSX elements.
3
Lift state: Pass count and setCount as props
Modify CounterParent to pass count as a prop named count to DisplayCount. Pass setCount as a prop named setCount to Incrementer. Update DisplayCount to accept count as a prop and display it inside the div. Update Incrementer to accept setCount as a prop and add an onClick handler to the button that calls setCount with count => count + 1.
React
Hint
Pass props from CounterParent and use them inside the child components.
4
Add accessibility and final touches
Add an aria-label attribute with value Increment counter to the button in Incrementer for accessibility. Wrap the two child components inside a section element with aria-live="polite" in CounterParent to announce count changes to screen readers.
React
Hint
Use aria-label on the button and wrap children in a section with aria-live="polite".
Practice
(1/5)
1. What is the main reason to lift state up in React? lifting state means moving state to a common parent component.
easy
A. To make components independent
B. To share state between sibling components
C. To avoid using props
D. To store state only in child components
Solution
Step 1: Understand lifting state concept
Lifting state means moving state to a common parent so multiple children can access it.
Step 2: Identify the purpose of lifting state
This is done to share state between sibling components and keep data in sync.
Final Answer:
To share state between sibling components -> Option B
Quick Check:
Lifting state = sharing state [OK]
Hint: Lifting state shares data between siblings via parent [OK]
Common Mistakes:
Thinking lifting state makes components independent
Believing lifting state removes props usage
Assuming state stays only in children
2. Which of the following is the correct way to pass state and its updater function from a parent to a child component in React?
easy
A. <Child state={state} setState={setState} />
B. <Child state=setState />
C. <Child setState={state} state={setState} />
D. <Child state=setState() />
Solution
Step 1: Recall React props syntax
Props are passed as name={value}. To pass state and updater, use state={state} and setState={setState}.
Step 2: Check each option's syntax
<Child state={state} setState={setState} /> correctly passes both as props. Others misuse assignment or call functions incorrectly.
Final Answer:
<Child state={state} setState={setState} /> -> Option A
A. setValue prop is incorrectly assigned the state value instead of the updater function
B. value state is not initialized properly
C. Child component is missing parentheses
D. React.useState should be React.state
Solution
Step 1: Check props passed to Child
Parent passes value={value} correctly but setValue={value} incorrectly passes the state value instead of the updater function.
Step 2: Understand correct usage
setValue should be passed as setValue={setValue} to allow Child to update state.
Final Answer:
setValue prop is incorrectly assigned the state value instead of the updater function -> Option A
Quick Check:
Updater function must be passed, not state value [OK]
Hint: Pass updater function, not state value, as setState prop [OK]
Common Mistakes:
Confusing state variable with updater function
Forgetting curly braces around variables
Assuming React.useState is incorrect
5. You have two sibling components, InputA and InputB, both need to update and display the same text value. Which pattern correctly lifts state to their parent to keep them in sync?
hard
A. Use local variables in siblings without state
B. Store text state separately in each sibling, update independently
C. Store text state in one sibling and pass it down to the other sibling
D. Store text state in parent, pass text and setText to both siblings as props
Solution
Step 1: Understand shared state need
Both siblings must show and update the same text, so state must be shared.
Step 2: Apply lifting state pattern
Lift text state to their common parent and pass text and setText as props to both siblings.
Step 3: Evaluate other options
Storing state separately or in one sibling breaks sync; local variables don't persist or trigger re-renders.
Final Answer:
Store text state in parent, pass text and setText to both siblings as props -> Option D
Quick Check:
Lift state to parent for shared updates [OK]
Hint: Lift shared state to parent, pass state and updater down [OK]
Common Mistakes:
Keeping separate states in siblings causing out-of-sync
Passing state only one way between siblings
Using local variables instead of state
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.