0
0
Reactframework~20 mins

Why lifting state is needed in React - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
State Lifting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2: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?
AIt allows both siblings to access and update the shared state from a single source of truth.
BIt duplicates the state in both siblings to keep them independent.
CIt moves the state to a global variable outside React components.
DIt prevents any component from updating the state to avoid bugs.
Attempts:
2 left
💡 Hint
Think about how components communicate and keep data consistent.
component_behavior
intermediate
2: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?
AThe components will merge their states behind the scenes.
BThey will have inconsistent data because each manages its own copy.
CThey will automatically sync their states without extra code.
DReact will throw an error about duplicate states.
Attempts:
2 left
💡 Hint
Think about what happens when two people keep separate notes about the same thing.
state_output
advanced
2: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>;
}
AClicking the button does not update any count because state is not shared.
BClicking the button updates only the button label, display stays the same.
CClicking the button updates the count in Parent, so both button label and display show the new count.
DClicking the button updates only the display, button label stays the same.
Attempts:
2 left
💡 Hint
State is held in Parent and passed down as props to both children.
📝 Syntax
advanced
2: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?
AParent holds state and passes value and setter as props to children; children call setter to update state.
BChildren hold their own state and pass it up to Parent via props.
CParent holds state but children create their own independent states ignoring props.
DState is declared inside children and Parent does not manage any state.
Attempts:
2 left
💡 Hint
Remember: lifting state means the parent owns the state and children receive it via props.
🔧 Debug
expert
3: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>;
}
AReact.useState is used incorrectly; it should be React.state.
BThe setCount function is not called correctly because onClick is missing parentheses.
CThe ChildButton does not receive the onClick prop properly, so button does nothing.
DThe increment function closes over the initial count value, so setCount always sets count to 1.
Attempts:
2 left
💡 Hint
Think about how JavaScript closures capture variables in functions.