0
0
Reactframework~20 mins

Creating context in React - Practice Exercises

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

Consider this React code using context:

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

const MyContext = createContext('default');

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

export default function App() {
  return (
    <MyContext.Provider value="hello">
      <Display />
    </MyContext.Provider>
  );
}

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: {value}</div>;
}

export default function App() {
  return (
    <MyContext.Provider value="hello">
      <Display />
    </MyContext.Provider>
  );
}
AValue: default
BValue: null
CValue: hello
DValue: undefined
Attempts:
2 left
💡 Hint

Check what value is passed to the Provider and what useContext reads.

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

Given this React component using context and state:

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>
  );
}

After clicking the button two times, what is the value of count shown?

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>
  );
}
ACount: 2
BCount: 1
CCount: 0
DCount: NaN
Attempts:
2 left
💡 Hint

Each click increments count by 1 starting from 0.

📝 Syntax
advanced
2:00remaining
Which option correctly creates a React context with a default value?

Choose the correct code to create a React context with default value "guest".

Aconst UserContext = createContext({ default: 'guest' });
Bconst UserContext = createContext('guest');
Cconst UserContext = createContext(); UserContext.defaultValue = 'guest';
Dconst UserContext = createContext; UserContext('guest');
Attempts:
2 left
💡 Hint

Look for the correct way to call createContext with a default value.

🔧 Debug
advanced
2:00remaining
Why does this React context example cause an error?

Examine this code snippet:

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

const ThemeContext = createContext();

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

export default function App() {
  return <DisplayTheme />;
}

What error will occur when this runs?

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

const ThemeContext = createContext();

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

export default function App() {
  return <DisplayTheme />;
}
ANo error, renders 'Theme: undefined'
BSyntaxError: Unexpected token
CReferenceError: ThemeContext is not defined
DTypeError: Cannot read property 'color' of undefined
Attempts:
2 left
💡 Hint

Check what value useContext returns when no Provider is used.

🧠 Conceptual
expert
2:00remaining
Which option best explains React context behavior when Provider value changes?

In React, when a context Provider's value prop changes, what happens to components consuming that context?

AConsuming components re-render only if the new context value is a different reference than before.
BOnly consuming components that use <code>useMemo</code> re-render with the new value.
CAll consuming components re-render immediately with the new context value.
DConsuming components never re-render automatically; you must force update manually.
Attempts:
2 left
💡 Hint

Think about how React compares context values to decide re-rendering.