0
0
Reactframework~20 mins

Context provider in React - Practice Problems & Coding Challenges

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

Consider this React code using a context provider and consumer. What will be rendered inside the <div>?

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

const MyContext = createContext('default');

function Display() {
  const value = useContext(MyContext);
  return <div>{value}</div>;
}

export default function App() {
  return (
    <MyContext.Provider value="Hello World">
      <Display />
    </MyContext.Provider>
  );
}
A<div>Hello World</div>
B<div></div>
C<div>undefined</div>
D<div>default</div>
Attempts:
2 left
💡 Hint

Remember that the value passed to the provider is what the consumer receives.

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

Given this React component using context and 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();

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

export default function App() {
  const [count, setCount] = useState(0);
  return (
    <CountContext.Provider value={{ count, setCount }}>
      <Counter />
    </CountContext.Provider>
  );
}
A0
B1
C2
Dundefined
Attempts:
2 left
💡 Hint

Each click increases count by 1 using the setter from context.

📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in defining a context provider?

Which of these React context provider usages will cause a syntax error?

A<MyContext.Provider value={value} children={children} />
B<MyContext.Provider value={value} children>{children}</MyContext.Provider>
C<MyContext.Provider value={value}>{children}</MyContext.Provider>
D<MyContext.Provider value={value} children={children}></MyContext.Provider>
Attempts:
2 left
💡 Hint

Check the usage of the children prop in JSX.

🔧 Debug
advanced
2:00remaining
Why does the consumer not update when provider value changes?

In this React code, the consumer component does not update when the provider's value changes. What is the most likely cause?

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

const ThemeContext = createContext('light');

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

export default function App() {
  const [theme, setTheme] = useState('light');

  function toggleTheme() {
    setTheme(theme === 'light' ? 'dark' : 'light');
  }

  return (
    <ThemeContext.Provider value={theme}>
      <ThemeDisplay />
      <button onClick={toggleTheme}>Toggle Theme</button>
    </ThemeContext.Provider>
  );
}
AState setter function is not called properly in toggleTheme.
BThemeContext is not created with a default value.
CuseContext hook is used incorrectly inside ThemeDisplay.
DProvider value is hardcoded as "light" and does not use state variable.
Attempts:
2 left
💡 Hint

Check what value the provider actually passes to consumers.

🧠 Conceptual
expert
3:00remaining
What happens if multiple nested providers supply different values?

Given nested React context providers with different values, what value does a consumer inside the inner provider receive?

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

const UserContext = createContext('Guest');

function ShowUser() {
  const user = useContext(UserContext);
  return <div>User: {user}</div>;
}

export default function App() {
  return (
    <UserContext.Provider value="Alice">
      <UserContext.Provider value="Bob">
        <ShowUser />
      </UserContext.Provider>
    </UserContext.Provider>
  );
}
A<div>User: Bob</div>
B<div>User: undefined</div>
C<div>User: Guest</div>
D<div>User: Alice</div>
Attempts:
2 left
💡 Hint

Think about how React context resolves values with nested providers.