0
0
NextJSframework~20 mins

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

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