Bird
Raised Fist0
NextJSframework~20 mins

React context in client components in NextJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
React Context Mastery
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 context usage in a client component?

Consider this Next.js client component using React context. What will be rendered inside the <p> tag?

NextJS
'use client';

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

const ThemeContext = createContext('light');


function DisplayTheme() {
  const theme = useContext(ThemeContext);
  return <p>{`Current theme is: ${theme}`}</p>;
}

export default function Page() {
  return (
    <ThemeContext.Provider value="dark">
      <DisplayTheme />
    </ThemeContext.Provider>
  );
}
ACurrent theme is: dark
BCurrent theme is: undefined
CError: useContext can only be used inside a Provider
DCurrent theme is: light
Attempts:
2 left
💡 Hint

Remember that the value passed to the Provider overrides the default context value.

❓ lifecycle
intermediate
2:00remaining
When does a React client component re-render when context changes?

Given a React client component consuming context, when will it re-render?

NextJS
'use client';

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

const CountContext = createContext(0);


function ShowCount() {
  const count = useContext(CountContext);
  return <p>{`Count is ${count}`}</p>;
}

export default function Counter() {
  const [count, setCount] = useState(0);

  return (
    <CountContext.Provider value={count}>
      <ShowCount />
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </CountContext.Provider>
  );
}
AShowCount re-renders only when the button is clicked twice
BShowCount never re-renders because context value is primitive
CShowCount re-renders every time the count state changes
DShowCount re-renders only on initial render
Attempts:
2 left
💡 Hint

Think about how React context triggers updates when the Provider's value changes.

🔧 Debug
advanced
2:00remaining
Why does this React client component throw an error using context?

Examine this code snippet. Why does it throw an error when rendering?

NextJS
'use client';

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

const UserContext = createContext(null);

function UserName() {
  const user = useContext(UserContext);
  return <p>{user.name}</p>;
}

export default function Page() {
  return <UserName />;
}
AError because UserName uses useContext without a Provider wrapping it
BError because createContext was called with null instead of an object
CNo error, renders empty paragraph
DError because useContext must be called inside a useEffect hook
Attempts:
2 left
💡 Hint

Check if the component consuming context is wrapped in a Provider.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a React context and uses it in a client component?

Choose the option that correctly creates a React context and consumes it in a client component without syntax errors.

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

const MyContext = createContext('default');

'use client';

function Comp() {
  const val = useContext(MyContext);
  return &lt;div&gt;{val}&lt;/div&gt;;
}

export default function Page() {
  return &lt;MyContext.Provider value="hello"&gt;&lt;Comp /&gt;&lt;/MyContext.Provider&gt;;
}
B
import React, { createContext, useContext } from 'react'

const MyContext = createContext('default')

function Comp() {
  const val = useContext(MyContext)
  return &lt;div&gt;{val}&lt;/div&gt;
}

export default function Page() {
  return &lt;MyContext.Provider value="hello"&gt;&lt;Comp /&gt;&lt;/MyContext.Provider&gt;
}
C
import React, { createContext, useContext } from 'react';

const MyContext = createContext('default');

function Comp() {
  const val = useContext(MyContext);
  return &lt;div&gt;{val}&lt;/div&gt;;
}

export default function Page() {
  return &lt;MyContext.Provider value="hello"&gt;&lt;Comp /&gt;&lt;/MyContext.Provider&gt;;
}
D
'use client';

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

const MyContext = createContext('default');


function Comp() {
  const val = useContext(MyContext);
  return &lt;div&gt;{val}&lt;/div&gt;;
}

export default function Page() {
  return &lt;MyContext.Provider value="hello"&gt;&lt;Comp /&gt;&lt;/MyContext.Provider&gt;;
}
Attempts:
2 left
💡 Hint

Remember to mark client components with 'use client'; directive.

❓ state_output
expert
2:00remaining
What is the final rendered output after clicking the button twice in this Next.js client component using context?

Analyze the code below. After clicking the "Add" button twice, what will be the rendered output inside the <div>?

NextJS
'use client';

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

const NumberContext = createContext(0);


function DisplayNumber() {
  const number = useContext(NumberContext);
  return <div>{`Number: ${number}`}</div>;
}

export default function Counter() {
  const [num, setNum] = useState(0);

  const add = useCallback(() => {
    setNum(n => n + 1);
  }, []);

  return (
    <NumberContext.Provider value={num}>
      <DisplayNumber />
      <button onClick={add}>Add</button>
    </NumberContext.Provider>
  );
}
ANumber: 0
BNumber: 2
CNumber: 1
DNumber: undefined
Attempts:
2 left
💡 Hint

Consider how state updates and context value changes affect re-rendering.

Practice

(1/5)
1. What is the main purpose of React context in client components?
easy
A. To share data easily without passing props through every component
B. To create new HTML elements dynamically
C. To style components with CSS
D. To handle browser events like clicks and scrolls

Solution

  1. Step 1: Understand React context role

    React context allows sharing data across components without manually passing props at every level.
  2. Step 2: Compare with other options

    Creating HTML elements, styling, and event handling are unrelated to React context's main purpose.
  3. Final Answer:

    To share data easily without passing props through every component -> Option A
  4. Quick Check:

    React context = data sharing without prop drilling [OK]
Hint: Context shares data without passing props everywhere [OK]
Common Mistakes:
  • Confusing context with styling or event handling
  • Thinking context creates HTML elements
  • Believing context is for CSS only
2. Which of the following is the correct way to access React context in a client component?
easy
A. const value = useRef(MyContext);
B. const value = useState(MyContext);
C. const value = useContext(MyContext);
D. const value = useEffect(MyContext);

Solution

  1. Step 1: Identify the hook for context access

    React provides the useContext hook to access context values inside client components.
  2. Step 2: Eliminate other hooks

    useState manages state, useEffect handles side effects, and useRef manages references, none access context.
  3. Final Answer:

    const value = useContext(MyContext); -> Option C
  4. Quick Check:

    useContext hook = context access [OK]
Hint: Use useContext to read context in components [OK]
Common Mistakes:
  • Using useState or useEffect instead of useContext
  • Trying to access context without a hook
  • Confusing useRef with context access
3. Given this code snippet in a Next.js client component:
import { createContext, useContext } from 'react';

const ThemeContext = createContext('light');

export default function DisplayTheme() {
  const theme = useContext(ThemeContext);
  return <div>Current theme: {theme}</div>;
}

What will be displayed if DisplayTheme is rendered without a ThemeContext.Provider?
medium
A. Current theme: undefined
B. Current theme: light
C. Current theme: null
D. Error: Context provider missing

Solution

  1. Step 1: Check default context value

    The context is created with default value 'light', so if no Provider wraps the component, this default is used.
  2. Step 2: Understand useContext behavior without Provider

    useContext returns the default value when no Provider is found above in the tree.
  3. Final Answer:

    Current theme: light -> Option B
  4. Quick Check:

    Default context value used without Provider [OK]
Hint: Default context value shows if no Provider wraps component [OK]
Common Mistakes:
  • Expecting undefined or null instead of default value
  • Thinking it throws an error without Provider
  • Confusing default value with null
4. Identify the error in this Next.js client component using React context:
import { createContext, useContext } from 'react';

const UserContext = createContext();

export default function UserName() {
  const user = useContext(UserContext);
  return <div>User: {user.name}</div>;
}
medium
A. UserContext is not wrapped with a Provider, so user is undefined causing error
B. useContext hook is used incorrectly; should pass a string
C. Missing import of React from 'react'
D. Cannot use context inside client components

Solution

  1. Step 1: Check context default value and Provider usage

    UserContext is created without a default value and no Provider is shown wrapping UserName, so useContext returns undefined.
  2. Step 2: Understand error cause

    Accessing user.name when user is undefined causes a runtime error.
  3. Final Answer:

    UserContext is not wrapped with a Provider, so user is undefined causing error -> Option A
  4. Quick Check:

    Undefined context value causes error accessing properties [OK]
Hint: Always wrap context consumers with Provider to avoid undefined errors [OK]
Common Mistakes:
  • Assuming useContext needs a string argument
  • Forgetting to wrap with Provider
  • Thinking React import is mandatory in Next.js 13+ client components
5. You want to create a theme toggle in a Next.js client component using React context. Which approach correctly updates the theme value across components?
hard
A. Pass theme as a prop to every component without using context
B. Create ThemeContext with a fixed string; update theme by changing the string directly in components
C. Use useState inside each component separately without context to manage theme
D. Create a ThemeContext with state and provide toggle function via Provider; use useContext to read and update theme

Solution

  1. Step 1: Understand state sharing with context

    To update theme globally, context must provide state and a function to toggle it, so all components share and update the same state.
  2. Step 2: Evaluate other options

    Fixed string cannot update dynamically; separate useState per component does not share state; passing props everywhere defeats context purpose.
  3. Final Answer:

    Create a ThemeContext with state and provide toggle function via Provider; use useContext to read and update theme -> Option D
  4. Quick Check:

    Context with state and updater shares dynamic data [OK]
Hint: Use context Provider with state and updater for shared dynamic data [OK]
Common Mistakes:
  • Trying to update fixed context value directly
  • Using separate state in each component without sharing
  • Passing props everywhere instead of using 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