0
0
Reactframework~20 mins

Sharing state between components in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
React State Sharing 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 pair?

Consider two components: Parent holds a state and passes it to Child. What will Child display?

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

function Child({ message }) {
  return <p>{message}</p>;
}

function Parent() {
  const [msg] = useState('Hello from Parent');
  return <Child message={msg} />;
}
AA runtime error occurs because 'message' prop is missing.
BAn empty paragraph is displayed with no text.
CThe text 'Hello from Parent' is displayed inside a paragraph.
DThe component renders nothing because state is not passed.
Attempts:
2 left
💡 Hint

Look at how Parent passes the msg state as a prop to Child.

state_output
intermediate
2:00remaining
What is the value of shared state after both components update it?

Two sibling components share a state lifted to their parent. Both update the state once. What is the final state value?

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

function SiblingA({ count, setCount }) {
  return <button onClick={() => setCount(count + 1)}>Increment A</button>;
}

function SiblingB({ count, setCount }) {
  return <button onClick={() => setCount(count + 2)}>Increment B</button>;
}

function Parent() {
  const [count, setCount] = useState(0);
  // Simulate both buttons clicked once
  React.useEffect(() => {
    setCount(count + 1);
    setCount(count + 2);
  }, []);
  return (
    <>
      <SiblingA count={count} setCount={setCount} />
      <SiblingB count={count} setCount={setCount} />
      <p>{count}</p>
    </>
  );
}
A2
B1
C3
D0
Attempts:
2 left
💡 Hint

Remember that state updates inside useEffect with the same base value do not accumulate automatically.

📝 Syntax
advanced
2:00remaining
Which option correctly shares state using React Context?

Identify the correct way to create and use a React Context to share a string state between components.

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

const MyContext = React.createContext();

function Provider({ children }) {
  const [value, setValue] = useState('shared');
  return (
    <MyContext.Provider value={{ value, setValue }}>
      {children}
    </MyContext.Provider>
  );
}

function Consumer() {
  const context = useContext(MyContext);
  return <p>{context.value}</p>;
}
AUse <Consumer /> without <Provider>, it displays 'shared'.
BWrap <Consumer /> inside <Provider> but forget to pass value prop, causes error.
CUse <Provider> without children, causes runtime error.
DWrap <Consumer /> inside <Provider> and it displays 'shared'.
Attempts:
2 left
💡 Hint

Context value must be passed via value prop on Provider.

🔧 Debug
advanced
2:00remaining
Why does this shared state not update in sibling components?

Given this code, why do sibling components not reflect updated shared state?

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

function SiblingA({ count }) {
  return <p>A: {count}</p>;
}

function SiblingB({ count }) {
  return <p>B: {count}</p>;
}

function Parent() {
  let count = 0;
  function increment() {
    count += 1;
  }
  return (
    <>
      <button onClick={increment}>Increment</button>
      <SiblingA count={count} />
      <SiblingB count={count} />
    </>
  );
}
AThe increment function is not called on button click.
BState is not managed with useState, so React does not re-render on change.
CSibling components do not receive the count prop.
DThe count variable is declared inside Parent, causing a syntax error.
Attempts:
2 left
💡 Hint

React only re-renders when state or props change via hooks or parent updates.

🧠 Conceptual
expert
3:00remaining
Which approach best shares complex state between deeply nested components?

You have a deeply nested component tree and want to share complex state (like user settings) across many components. Which approach is best?

AUse React Context to provide state and updater functions at a high level and consume where needed.
BUse local state in each component and synchronize manually via callbacks.
CUse global variables outside React and access them directly inside components.
DLift state up to the nearest common ancestor and pass props down through every level.
Attempts:
2 left
💡 Hint

Think about avoiding prop drilling and keeping state in sync easily.