0
0
Reactframework~20 mins

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

Choose your learning style9 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.