0
0
Reactframework~20 mins

Context best practices in React - Practice Problems & Coding Challenges

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 component using Context?

Consider this React code using Context. What will be rendered inside the <Display> component?

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

const MyContext = createContext('default');

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

function App() {
  return (
    <MyContext.Provider value="Hello, world!">
      <Display />
    </MyContext.Provider>
  );
}

export default App;
A<p>undefined</p>
BError: useContext must be inside a Provider
C<p>default</p>
D<p>Hello, world!</p>
Attempts:
2 left
💡 Hint

Think about what value the Context Provider passes down.

state_output
intermediate
2:00remaining
What is the final value of count after these context updates?

Given this React context and component, what is the final value of count displayed?

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

const CountContext = createContext();

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

function App() {
  const [count, setCount] = useState(0);
  return (
    <CountContext.Provider value={{ count, setCount }}>
      <Counter />
    </CountContext.Provider>
  );
}

export default App;
A0
B1
CInfinite loop error
DUndefined
Attempts:
2 left
💡 Hint

Consider what happens when setCount is called inside the component body.

📝 Syntax
advanced
2:00remaining
Which option correctly creates a Context with a default value and uses it?

Which code snippet correctly creates a React Context with a default value and consumes it inside a component?

A
const ThemeContext = createContext('light');
function Theme() {
  const theme = useContext(ThemeContext);
  return &lt;div&gt;{theme}&lt;/div&gt;;
}
B
const ThemeContext = createContext('dark');
function Theme() {
  const theme = useContext(ThemeContext, 'light');
  return &lt;div&gt;{theme}&lt;/div&gt;;
}
C
const ThemeContext = createContext();
function Theme() {
  const theme = useContext();
  return &lt;div&gt;{theme}&lt;/div&gt;;
}
D
const ThemeContext = createContext('dark');
function Theme() {
  const theme = ThemeContext.useContext();
  return &lt;div&gt;{theme}&lt;/div&gt;;
}
Attempts:
2 left
💡 Hint

Remember how useContext is called with the Context object.

🔧 Debug
advanced
2:00remaining
Why does this Context consumer component not update when Provider value changes?

Look at this React code. The Display component does not update when the Provider's value changes. Why?

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

const NumberContext = createContext(0);

function Display() {
  const value = useContext(NumberContext);
  return <p>{value}</p>;
}

function App() {
  const [num, setNum] = useState(0);

  function update() {
    setNum(num + 1);
  }

  return (
    <NumberContext.Provider value={num}>
      <Display />
      <button onClick={update}>Increase</button>
    </NumberContext.Provider>
  );
}

export default App;
AThe state variable 'num' is mutated directly instead of using setNum, so React does not re-render.
BThe Context Provider is missing a value prop.
CThe useContext hook is used outside of a Provider.
DThe Display component does not consume the context value.
Attempts:
2 left
💡 Hint

How should React state be updated to trigger re-render?

🧠 Conceptual
expert
2:00remaining
Which is the best practice for optimizing Context usage in React?

Which option describes the best practice to avoid unnecessary re-renders when using React Context?

AUse multiple Providers with the same context to isolate state updates.
BWrap only the components that need the context with the Provider and memoize context value objects to prevent re-renders.
CAvoid using React Context and pass props down manually to prevent re-renders.
DWrap the entire app with a single Provider and always pass new objects as context value to ensure fresh data.
Attempts:
2 left
💡 Hint

Think about how React decides to re-render components consuming context.