0
0
Reactframework~20 mins

Consuming context in React - Practice Problems & Coding Challenges

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

Given the following React context and component, what will be rendered inside the <div>?

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

const ThemeContext = createContext('light');

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

export default function App() {
  return (
    <ThemeContext.Provider value="dark">
      <DisplayTheme />
    </ThemeContext.Provider>
  );
}
A<div>Current theme is: dark</div>
BError: useContext must be inside a Provider
C<div>Current theme is: undefined</div>
D<div>Current theme is: light</div>
Attempts:
2 left
💡 Hint

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

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

Consider this React component consuming a context with a count state. What will be the value of count after clicking the button once?

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

const CountContext = createContext();

function Counter() {
  const { count, setCount } = useContext(CountContext);
  return <button onClick={() => setCount(count + 1)}>Increment</button>;
}

export default function App() {
  const [count, setCount] = useState(0);
  return (
    <CountContext.Provider value={{ count, setCount }}>
      <Counter />
      <p>{`Count is: ${count}`}</p>
    </CountContext.Provider>
  );
}
ACount is: 1
BCount is: 0
CCount is: undefined
DError: setCount is not a function
Attempts:
2 left
💡 Hint

Clicking the button calls setCount(count + 1) which updates the state.

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

Which code snippet correctly consumes a React context with a default value fallback when no Provider is used?

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

const UserContext = createContext('Guest');

function Welcome() {
  // Fill in the blank
  const user = /* ??? */;
  return <p>Welcome, {user}!</p>;
}
Aconst user = useContext();
Bconst user = UserContext.useContext();
Cconst user = useContext(UserContext);
Dconst user = UserContext.Consumer();
Attempts:
2 left
💡 Hint

Use the useContext hook with the context object as argument.

🔧 Debug
advanced
2:00remaining
Why does this component throw an error when consuming context?

Given this code, why does DisplayUser throw an error?

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

const UserContext = createContext();

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

export default function App() {
  return <DisplayUser />;
}
ADisplayUser must be a class component to consume context
BuseContext is called outside a Provider, which is not allowed and throws an error
CUserContext is not created with a default value, so useContext returns null causing error
Duser is undefined because no Provider is wrapping DisplayUser, so accessing user.name throws an error
Attempts:
2 left
💡 Hint

Check what useContext returns when no Provider is present.

🧠 Conceptual
expert
2:00remaining
Which option best describes React context consumption behavior?

Choose the statement that correctly describes how React context consumption works with useContext and Providers.

AuseContext always returns the default context value regardless of Providers
BuseContext returns the nearest Provider's value above in the tree or the default context value if no Provider exists
CuseContext requires a Provider to be present or it throws an error
DuseContext returns a copy of the Provider's value that cannot be updated
Attempts:
2 left
💡 Hint

Think about how context values propagate in React component trees.