Bird
Raised Fist0
Reactframework~10 mins

Consuming context in React - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Consuming context
Create Context
↓
Provide Context Value
↓
Component Tree
↓
Consume Context with useContext
↓
Render with Context Value
This flow shows how React context is created, provided to components, consumed using useContext, and then used to render UI.
Execution Sample
React
import React, { createContext, useContext } from 'react';

const ThemeContext = createContext('light');

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

export default DisplayTheme;
This code creates a ThemeContext with default 'light', then DisplayTheme component consumes it and shows the current theme.
Execution Table
StepActionContext ValueComponent RenderedOutput
1Create ThemeContext with default 'light'lightNoneNo output yet
2Render DisplayTheme without ProviderlightDisplayTheme<div>Theme is light</div>
3Render DisplayTheme inside Provider with value 'dark'darkDisplayTheme<div>Theme is dark</div>
4Change Provider value to 'blue'blueDisplayTheme<div>Theme is blue</div>
5Remove Provider, fallback to defaultlightDisplayTheme<div>Theme is light</div>
💡 Rendering stops after DisplayTheme shows the current context value based on Provider or default.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5
themeundefinedlightdarkbluelight
Key Moments - 3 Insights
Why does DisplayTheme show 'light' when no Provider is used?
Because the default value 'light' set in createContext is used when no Provider wraps the component, as shown in execution_table step 2.
How does changing the Provider value affect the DisplayTheme output?
Changing the Provider value updates the context value consumed by DisplayTheme, causing it to re-render with the new value, as seen in steps 3 and 4.
What happens if the Provider is removed after being used?
The context falls back to the default value from createContext, so DisplayTheme shows 'light' again, as in step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the context value when DisplayTheme renders without a Provider?
A'dark'
B'blue'
C'light'
Dundefined
💡 Hint
Check execution_table row 2 where no Provider is used.
At which step does DisplayTheme render with the context value 'blue'?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at execution_table row 4 for context value 'blue'.
If the Provider value changes from 'dark' to 'blue', what happens to the theme variable in DisplayTheme?
AIt stays 'dark'
BIt changes to 'blue'
CIt becomes undefined
DIt resets to default 'light'
💡 Hint
See variable_tracker column After Step 4.
Concept Snapshot
React context lets components share data without passing props.
Create context with createContext(defaultValue).
Wrap components with Provider to set context value.
Consume context inside components using useContext.
If no Provider, useContext returns default value.
Changing Provider value triggers re-render with new context.
Full Transcript
In React, context allows components to share data easily. First, you create a context with createContext and give it a default value. Then, you wrap parts of your component tree with a Provider that sets the context value. Inside any component, you use the useContext hook to get the current context value. If no Provider is used, the default value is returned. When the Provider's value changes, components consuming the context re-render to show the new value. This example shows a ThemeContext with default 'light'. DisplayTheme component reads the theme and displays it. Without a Provider, it shows 'light'. With Provider value 'dark' or 'blue', it updates accordingly. Removing Provider falls back to default again.

Practice

(1/5)
1. What is the primary purpose of using useContext in React?
easy
A. To fetch data from an API
B. To create a new context object
C. To update the state of a component
D. To read shared data from a context inside a component

Solution

  1. Step 1: Understand the role of useContext

    useContext is a React hook used to access data from a context object inside a component.
  2. Step 2: Differentiate from other hooks

    It does not create context or update state; it only reads the shared data provided by a context.
  3. Final Answer:

    To read shared data from a context inside a component -> Option D
  4. Quick Check:

    useContext reads context data [OK]
Hint: useContext reads shared data inside components [OK]
Common Mistakes:
  • Confusing useContext with creating context
  • Thinking useContext updates state
  • Using useContext to fetch API data
2. Which of the following is the correct syntax to consume a context named ThemeContext inside a functional component?
easy
A. const theme = useContext(ThemeContext);
B. const theme = ThemeContext.useContext();
C. const theme = useContext();
D. const theme = ThemeContext();

Solution

  1. Step 1: Recall the correct useContext syntax

    The hook useContext is called with the context object as an argument: useContext(ThemeContext).
  2. Step 2: Identify incorrect options

    Options A, B, and D misuse the hook or context object syntax.
  3. Final Answer:

    const theme = useContext(ThemeContext); -> Option A
  4. Quick Check:

    useContext requires context object as argument [OK]
Hint: Pass context object inside useContext() [OK]
Common Mistakes:
  • Calling useContext without argument
  • Using context object as a function
  • Trying to call useContext as a method on context
3. Given this code snippet, what will be logged to the console?
const UserContext = React.createContext('Guest');

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

function App() {
  return (
    <UserContext.Provider value="Alice">
      <DisplayUser />
    </UserContext.Provider>
  );
}
medium
A. "Guest"
B. "Alice"
C. undefined
D. Error: Context not found

Solution

  1. Step 1: Identify the context default and provider value

    The default value is "Guest" but the Provider sets the value to "Alice".
  2. Step 2: Understand useContext behavior inside Provider

    Since DisplayUser is inside the Provider, it receives the value "Alice" from context.
  3. Final Answer:

    "Alice" -> Option B
  4. Quick Check:

    Provider value overrides default context [OK]
Hint: Provider value overrides default context value [OK]
Common Mistakes:
  • Assuming default context value is always used
  • Thinking useContext returns undefined without explicit value
  • Confusing Provider and Consumer roles
4. What is the error in the following code that tries to consume a context?
const LanguageContext = React.createContext('English');

function Greeting() {
  const language = useContext(LanguageContext);
  return <h1>Hello in {language}</h1>;
}

function App() {
  return <Greeting />;
}
medium
A. LanguageContext must be a function, not an object
B. Greeting component must be a class component
C. useContext is not imported or defined
D. Context value must be passed as a prop

Solution

  1. Step 1: Check useContext usage

    The code uses useContext but does not import or prefix it with React.
  2. Step 2: Identify missing import or reference

    Without importing useContext from React or using React.useContext, this causes a ReferenceError.
  3. Final Answer:

    useContext is not imported or defined -> Option C
  4. Quick Check:

    useContext must be imported or accessed via React [OK]
Hint: Always import or prefix useContext with React [OK]
Common Mistakes:
  • Forgetting to import useContext
  • Using useContext without React prefix or import
  • Assuming context value passed as prop automatically
5. You have a nested component structure where ThemeContext provides a value at the top level. You want to consume this context in a deeply nested component without passing props through intermediate components. Which approach correctly achieves this?
hard
A. Wrap the nested component tree with ThemeContext.Provider and use useContext(ThemeContext) inside the nested component
B. Pass the context value as props through every intermediate component until it reaches the nested component
C. Use useContext in the nested component without wrapping the tree in ThemeContext.Provider
D. Create a new context inside the nested component and use useContext on it

Solution

  1. Step 1: Understand context provider role

    The ThemeContext.Provider must wrap the component tree to supply the context value.
  2. Step 2: Consume context in nested component

    Inside the nested component, useContext(ThemeContext) reads the value without needing props passed down.
  3. Final Answer:

    Wrap the nested component tree with ThemeContext.Provider and use useContext(ThemeContext) inside the nested component -> Option A
  4. Quick Check:

    Provider wraps tree; useContext reads value [OK]
Hint: Wrap tree with Provider, consume with useContext inside [OK]
Common Mistakes:
  • Trying to use useContext without Provider
  • Passing props through all components instead of context
  • Creating new context inside nested component
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