0
0
Reactframework~20 mins

Avoiding prop drilling in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Prop Drilling 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 tree using Context?

Consider the following React components using Context to avoid prop drilling. What will be rendered inside the <DisplayUser> component?

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

const UserContext = createContext(null);

function DisplayUser() {
  const user = useContext(UserContext);
  return <p>{`User: ${user.name}`}</p>;
}

function Wrapper() {
  const user = { name: 'Alice' };
  return (
    <UserContext.Provider value={user}>
      <DisplayUser />
    </UserContext.Provider>
  );
}

export default Wrapper;
AError: Cannot read property 'name' of null
B<p>User: undefined</p>
C<p>User: </p>
D<p>User: Alice</p>
Attempts:
2 left
💡 Hint

Look at how useContext gets the value from UserContext.Provider.

state_output
intermediate
2:00remaining
What is the value of count after clicking the button twice in this component using Context?

Given this React component using Context to share state, what will be the value of count after clicking the button two times?

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

const CountContext = createContext(null);

function IncrementButton() {
  const { count, setCount } = useContext(CountContext);
  return <button onClick={() => setCount(count + 1)}>Increment</button>;
}

function DisplayCount() {
  const { count } = useContext(CountContext);
  return <p>Count: {count}</p>;
}

function Counter() {
  const [count, setCount] = useState(0);
  return (
    <CountContext.Provider value={{ count, setCount }}>
      <DisplayCount />
      <IncrementButton />
    </CountContext.Provider>
  );
}

export default Counter;
ACount: 2
BCount: 1
CCount: 0
DError: setCount is not a function
Attempts:
2 left
💡 Hint

Each button click increases count by 1 using setCount.

🔧 Debug
advanced
2:00remaining
Why does this component fail to update when context value changes?

Look at this React code using Context. The DisplayTheme component does not update when the theme changes. What is the cause?

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

const ThemeContext = createContext('light');

function DisplayTheme() {
  const theme = useContext(ThemeContext);
  return <p>Theme: {theme}</p>;
}

function ThemeSwitcher() {
  const [theme, setTheme] = useState('light');

  const toggleTheme = useCallback(() => {
    setTheme(theme === 'light' ? 'dark' : 'light');
  }, []);

  return (
    <ThemeContext.Provider value={theme}>
      <DisplayTheme />
      <button onClick={toggleTheme}>Toggle Theme</button>
    </ThemeContext.Provider>
  );
}

export default ThemeSwitcher;
AThe <code>DisplayTheme</code> component does not consume the context properly.
BThe context value is primitive and React does not detect changes in primitive values.
CThe <code>toggleTheme</code> function uses stale closure over <code>theme</code> causing no update.
DThe <code>ThemeContext.Provider</code> is missing a key prop to trigger re-render.
Attempts:
2 left
💡 Hint

Think about how toggleTheme accesses theme inside the function.

🧠 Conceptual
advanced
1:30remaining
Which option best explains how React Context helps avoid prop drilling?

Prop drilling means passing props through many layers of components. How does React Context help avoid this?

AContext automatically updates all components in the app regardless of their position in the tree.
BContext allows components to share data directly without passing props through every intermediate component.
CContext replaces the need for state management by storing all data globally.
DContext forces all components to re-render whenever any data changes, improving consistency.
Attempts:
2 left
💡 Hint

Think about how data flows with and without Context.

📝 Syntax
expert
2:30remaining
Which option correctly creates a Context with a default value and consumes it in a functional component?

Choose the code snippet that correctly creates a React Context with default value 'blue' and uses it inside a component to display the color.

A
const ColorContext = createContext('blue');
function ShowColor() {
  const color = useContext(ColorContext);
  return &lt;p&gt;{color}&lt;/p&gt;;
}
B
const ColorContext = createContext();
function ShowColor() {
  const color = useContext(ColorContext, 'blue');
  return &lt;p&gt;{color}&lt;/p&gt;;
}
C
const ColorContext = createContext('blue');
function ShowColor() {
  const color = ColorContext.useContext();
  return &lt;p&gt;{color}&lt;/p&gt;;
}
D
const ColorContext = createContext('blue');
function ShowColor() {
  const color = useContext();
  return &lt;p&gt;{color}&lt;/p&gt;;
}
Attempts:
2 left
💡 Hint

Remember how to create and consume context with createContext and useContext.