Bird
Raised Fist0
Reactframework~20 mins

Context provider in React - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. What is the main purpose of a Context.Provider in React?
easy
A. To share data with all components inside its tree without passing props manually
B. To create a new React component
C. To handle component state locally
D. To style components globally

Solution

  1. Step 1: Understand what Context.Provider does

    It allows sharing data to all nested components without passing props down manually.
  2. Step 2: Compare options

    Only To share data with all components inside its tree without passing props manually describes this sharing behavior correctly; others describe unrelated tasks.
  3. Final Answer:

    To share data with all components inside its tree without passing props manually -> Option A
  4. Quick Check:

    Context.Provider shares data = A [OK]
Hint: Context.Provider shares data down component tree [OK]
Common Mistakes:
  • Thinking Provider creates components
  • Confusing Provider with state management
  • Assuming Provider styles components
2. Which of the following is the correct way to create a context and provide a value in React?
easy
A. const MyContext = React.createContext(); ...
B. const MyContext = React.useContext(); ...
C. const MyContext = React.createProvider(); ...
D. const MyContext = React.createContext(); ...

Solution

  1. Step 1: Identify correct context creation method

    React.createContext() is the correct method to create a context.
  2. Step 2: Confirm correct usage of Provider

    Provider uses a value prop to share data; Consumer does not accept value prop directly.
  3. Final Answer:

    const MyContext = React.createContext(); <MyContext.Provider value={data}>...</MyContext.Provider> -> Option D
  4. Quick Check:

    createContext + Provider with value = D [OK]
Hint: Use React.createContext() and Provider with value prop [OK]
Common Mistakes:
  • Using React.useContext() to create context
  • Trying React.createProvider() which doesn't exist
  • Passing value to Consumer instead of Provider
3. Given the code below, what will be rendered inside DisplayUser component?
const UserContext = React.createContext();

function DisplayUser() {
  const user = React.useContext(UserContext);
  return <div>Hello, {user.name}!</div>;
}

function App() {
  return (
    <UserContext.Provider value={{ name: 'Alice' }}>
      <DisplayUser />
    </UserContext.Provider>
  );
}
medium
A. Hello, undefined!
B. Error: useContext must be inside a Provider
C. Hello, Alice!
D. Hello, {user.name}!

Solution

  1. Step 1: Check Provider value

    The Provider passes an object with name 'Alice' as value.
  2. Step 2: useContext reads the value

    DisplayUser uses useContext to get the user object and accesses user.name which is 'Alice'.
  3. Final Answer:

    Hello, Alice! -> Option C
  4. Quick Check:

    useContext reads Provider value = B [OK]
Hint: useContext gets value from nearest Provider [OK]
Common Mistakes:
  • Assuming useContext returns undefined without Provider
  • Thinking useContext returns the whole context object, not value
  • Confusing JSX output with raw code
4. Identify the error in the following React code using Context Provider:
const ThemeContext = React.createContext();

function App() {
  return (
    <ThemeContext.Provider>
      <Content />
    </ThemeContext.Provider>
  );
}

function Content() {
  const theme = React.useContext(ThemeContext);
  return <div>Theme is {theme}</div>;
}
medium
A. Provider is missing the required value prop
B. useContext is used incorrectly outside Provider
C. ThemeContext should be created with React.useContext()
D. Content component should not use useContext

Solution

  1. Step 1: Check Provider usage

    The Provider must have a value prop to supply data to consumers.
  2. Step 2: Identify missing value prop

    In the code, <ThemeContext.Provider> lacks value, so useContext gets undefined.
  3. Final Answer:

    Provider is missing the required value prop -> Option A
  4. Quick Check:

    Provider needs value prop = C [OK]
Hint: Provider always needs a value prop [OK]
Common Mistakes:
  • Forgetting to pass value to Provider
  • Confusing createContext with useContext
  • Thinking useContext can't be inside Provider tree
5. You want to share a user object and a function to update it across many components using React Context. Which approach correctly combines these in a single context provider?
hard
A. Create two separate contexts, one for user and one for setUser; use two Providers
B. Create context with React.createContext(); provide value={{ user, setUser }}; consume with useContext
C. Pass user as value and call setUser directly inside components without context
D. Use useState inside each component instead of context

Solution

  1. Step 1: Understand combining data and updater in context

    Context value can be an object holding both user data and setUser function.
  2. Step 2: Confirm correct usage

    Providing value={{ user, setUser }} allows any component to read user and update it via setUser.
  3. Final Answer:

    Create context with React.createContext(); provide value={{ user, setUser }}; consume with useContext -> Option B
  4. Quick Check:

    Combine data and updater in one context value = A [OK]
Hint: Put data and updater together in context value object [OK]
Common Mistakes:
  • Creating multiple contexts unnecessarily
  • Calling setUser without context
  • Using local state instead of shared context
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