Challenge - 5 Problems
State Lifting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Why do we lift state up in React?
Imagine two sibling components need to share some data. Why is lifting state up to their common parent useful?
Attempts:
2 left
💡 Hint
Think about how components communicate and keep data consistent.
✗ Incorrect
Lifting state up means moving shared state to the closest common parent. This way, siblings get the same data from one place, making updates consistent and easier to manage.
❓ component_behavior
intermediate2:00remaining
What happens if state is not lifted up?
Two sibling components each have their own state for the same data. What problem will likely occur?
Attempts:
2 left
💡 Hint
Think about what happens when two people keep separate notes about the same thing.
✗ Incorrect
If siblings keep separate state copies, changes in one won't reflect in the other. This causes UI inconsistencies and bugs.
❓ state_output
advanced2:30remaining
What is the output after lifting state up?
Given a parent component holding a count state and passing it to two child buttons, what happens when one button increments the count?
React
function Parent() { const [count, setCount] = React.useState(0); return ( <> <ChildButton count={count} onClick={() => setCount(count + 1)} /> <ChildDisplay count={count} /> </> ); } function ChildButton({ count, onClick }) { return <button onClick={onClick}>Increment: {count}</button>; } function ChildDisplay({ count }) { return <p>Current count: {count}</p>; }
Attempts:
2 left
💡 Hint
State is held in Parent and passed down as props to both children.
✗ Incorrect
Since the count state is in Parent, updating it triggers re-render of both children. Both show the updated count.
📝 Syntax
advanced2:00remaining
Identify the correct way to lift state up in React
Which code snippet correctly lifts state up to a parent component and passes it down to children?
Attempts:
2 left
💡 Hint
Remember: lifting state means the parent owns the state and children receive it via props.
✗ Incorrect
Correct lifting means the parent owns the state and passes it down. Children use props to read and callbacks to update.
🔧 Debug
expert3:00remaining
Why does this lifted state example cause a stale closure bug?
In this code, clicking the button does not update the count as expected. What is the cause?
React
function Parent() { const [count, setCount] = React.useState(0); const increment = React.useCallback(() => { setCount(count + 1); }, []); return <ChildButton onClick={increment} />; } function ChildButton({ onClick }) { return <button onClick={onClick}>Increment</button>; }
Attempts:
2 left
💡 Hint
Think about how JavaScript closures capture variables in functions.
✗ Incorrect
The increment function captures the count value when Parent renders. It does not see updated count on later clicks, causing stale state updates.