Bird
Raised Fist0
Reactframework~20 mins

Common lifting state patterns in React - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
React State Lifting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output of this React component when clicking the button?

Consider a parent component that lifts state up to share a counter value between two child components. What will be displayed after clicking the increment button once?

React
import React, { useState } from 'react';

function Parent() {
  const [count, setCount] = useState(0);
  return (
    <>
      <Display count={count} />
      <IncrementButton onIncrement={() => setCount(count + 1)} />
    </>
  );
}

function Display({ count }) {
  return <p>Count: {count}</p>;
}

function IncrementButton({ onIncrement }) {
  return <button onClick={onIncrement}>Increment</button>;
}

export default Parent;
ACount: 1
BCount: 0
CCount: undefined
DButton does not work
Attempts:
2 left
💡 Hint

Think about how the state updates and re-renders the display component.

❓ state_output
intermediate
2:00remaining
What is the value of count after clicking the button twice quickly?

Given this React component lifting state up, what will be the final count value after clicking the increment button twice quickly?

React
import React, { useState } from 'react';

function Parent() {
  const [count, setCount] = useState(0);
  const increment = () => {
    setCount(count + 1);
  };
  return (
    <>
      <p>Count: {count}</p>
      <button onClick={increment}>Increment</button>
    </>
  );
}

export default Parent;
ACount: 0
BCount: 2
CCount: 1
DCount: NaN
Attempts:
2 left
💡 Hint

Consider how React batches state updates and the closure over count.

📝 Syntax
advanced
2:30remaining
Which option correctly lifts state up to share input value between siblings?

You want two sibling components to share the same input value by lifting state up to their parent. Which code snippet correctly implements this pattern?

A
function Parent() {
  const [value, setValue] = useState('');
  return (
    &lt;&gt;
      &lt;Input value={value} onChange={e =&gt; setValue(e.target.value)} /&gt;
      &lt;Display value={value} /&gt;
    &lt;/&gt;
  );
}

function Input({ value, onChange }) {
  return &lt;input value={value} onChange={onChange} /&gt;;
}

function Display({ value }) {
  return &lt;p&gt;{value}&lt;/p&gt;;
}
B
function Parent() {
  const [value, setValue] = useState('');
  return (
    &lt;&gt;
      &lt;Input defaultValue={value} onChange={e =&gt; setValue(e.target.value)} /&gt;
      &lt;Display value={value} /&gt;
    &lt;/&gt;
  );
}

function Input({ defaultValue, onChange }) {
  return &lt;input defaultValue={defaultValue} onChange={onChange} /&gt;;
}

function Display({ value }) {
  return &lt;p&gt;{value}&lt;/p&gt;;
}
C
function Parent() {
  const [value, setValue] = useState('');
  return (
    &lt;&gt;
      &lt;Input onChange={e =&gt; setValue(e.target.value)} /&gt;
      &lt;Display value={value} /&gt;
    &lt;/&gt;
  );
}

function Input({ onChange }) {
  return &lt;input onChange={onChange} /&gt;;
}

function Display({ value }) {
  return &lt;p&gt;{value}&lt;/p&gt;;
}
D
function Parent() {
  const [value, setValue] = useState('');
  return (
    &lt;&gt;
      &lt;Input value={value} /&gt;
      &lt;Display value={value} /&gt;
    &lt;/&gt;
  );
}

function Input({ value }) {
  return &lt;input value={value} /&gt;;
}

function Display({ value }) {
  return &lt;p&gt;{value}&lt;/p&gt;;
}
Attempts:
2 left
💡 Hint

Controlled inputs need value and onChange props to update state.

🔧 Debug
advanced
2:30remaining
Why does the child's useEffect not log updates when parent state changes?

Given this React code lifting state up, the console.log in the child component's useEffect does not show updates when the parent state changes. What is the cause?

React
function Parent() {
  const [text, setText] = React.useState('');
  return (
    <>
      <input onChange={e => setText(e.target.value)} />
      <Child text={text} />
    </>
  );
}

function Child({ text }) {
  React.useEffect(() => {
    console.log('Text changed:', text);
  }, []);
  return <p>{text}</p>;
}
AThe input element is missing a value prop, so it cannot update state.
BThe Child's useEffect dependency array is empty, so it does not run on text changes.
CThe Parent does not pass the text prop correctly to Child.
DThe Child component does not render the text prop.
Attempts:
2 left
💡 Hint

Check the dependencies of useEffect in the child component.

🧠 Conceptual
expert
3:00remaining
Which pattern best describes lifting state up in React?

When multiple sibling components need to share and update the same data, what is the best React pattern to manage this?

AEach sibling manages its own state independently and uses context to sync values.
BDuplicate the state in each sibling and synchronize them with event listeners.
CUse global variables outside React components to hold shared state and access them directly.
DStore the shared state in their closest common parent and pass it down as props with callbacks to update it.
Attempts:
2 left
💡 Hint

Think about React's recommended way to share state between siblings.

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

  1. Step 1: Understand lifting state concept

    Lifting state means moving state to a common parent so multiple children can access it.
  2. Step 2: Identify the purpose of lifting state

    This is done to share state between sibling components and keep data in sync.
  3. Final Answer:

    To share state between sibling components -> Option B
  4. 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

  1. Step 1: Recall React props syntax

    Props are passed as name={value}. To pass state and updater, use state={state} and setState={setState}.
  2. Step 2: Check each option's syntax

    <Child state={state} setState={setState} /> correctly passes both as props. Others misuse assignment or call functions incorrectly.
  3. Final Answer:

    <Child state={state} setState={setState} /> -> Option A
  4. Quick Check:

    Props pass state and updater correctly [OK]
Hint: Props use curly braces for variables [OK]
Common Mistakes:
  • Passing functions without curly braces
  • Calling updater functions instead of passing them
  • Swapping state and updater names
3. Given this parent component code:
function Parent() {
  const [count, setCount] = React.useState(0);
  return (
    <>
      <Display count={count} />
      <Button onClick={() => setCount(count + 1)} />
    </>
  );
}

What will happen when the Button is clicked?
medium
A. Nothing happens because count is not updated
B. The Button component will re-render but count stays the same
C. The count displayed by Display increases by 1
D. An error occurs because setCount is not passed to Button

Solution

  1. Step 1: Understand state and props flow

    The Parent holds count state and passes count to Display and an onClick handler to Button.
  2. Step 2: Analyze Button click effect

    Clicking Button calls setCount(count + 1), updating state and causing Parent to re-render with new count.
  3. Final Answer:

    The count displayed by Display increases by 1 -> Option C
  4. Quick Check:

    State update triggers re-render and display update [OK]
Hint: State update in parent re-renders children with new props [OK]
Common Mistakes:
  • Thinking Button needs count prop to update
  • Believing setCount must be passed to Display
  • Assuming no re-render happens on state change
4. Identify the error in this code snippet where a parent tries to lift state:
function Parent() {
  const [value, setValue] = React.useState('');
  return <Child value={value} setValue={value} />;
}
medium
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

  1. 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.
  2. Step 2: Understand correct usage

    setValue should be passed as setValue={setValue} to allow Child to update state.
  3. Final Answer:

    setValue prop is incorrectly assigned the state value instead of the updater function -> Option A
  4. 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

  1. Step 1: Understand shared state need

    Both siblings must show and update the same text, so state must be shared.
  2. Step 2: Apply lifting state pattern

    Lift text state to their common parent and pass text and setText as props to both siblings.
  3. Step 3: Evaluate other options

    Storing state separately or in one sibling breaks sync; local variables don't persist or trigger re-renders.
  4. Final Answer:

    Store text state in parent, pass text and setText to both siblings as props -> Option D
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard