React Context is used to share data across components without passing props manually at every level. It helps avoid 'prop drilling' by providing a way to pass data through the component tree.
Click to reveal answer
intermediate
Why should you avoid putting large or frequently changing data in Context?
Putting large or frequently changing data in Context can cause unnecessary re-renders of all components consuming that context, which can hurt performance. It's better to keep Context for stable, global data.
Click to reveal answer
intermediate
How can you optimize Context usage to prevent unnecessary re-renders?
You can optimize by splitting Context into smaller, focused contexts, memoizing context values with useMemo, and avoiding passing new object or function references unless needed.
Click to reveal answer
beginner
What is a good practice when naming Context providers and consumers?
Use clear and descriptive names for Context and its Provider, like ThemeContext and ThemeProvider. This makes code easier to read and understand.
Click to reveal answer
beginner
When should you consider using Context over props?
Use Context when you need to share data that many components need, especially deeply nested ones, and when passing props through many layers becomes cumbersome.
Click to reveal answer
What problem does React Context primarily solve?
AImproving CSS styling
BAvoiding prop drilling
CHandling HTTP requests
DManaging component lifecycle
✗ Incorrect
React Context helps avoid prop drilling by allowing data to be shared without passing props through every component.
Which of these is a best practice when using React Context?
APut all app state in one big Context
BUse Context only for styling
CNever memoize context values
DSplit Context into smaller focused parts
✗ Incorrect
Splitting Context into smaller parts helps optimize performance and keeps code organized.
What can cause unnecessary re-renders when using Context?
APassing new object references every render
BMemoizing context values
CPassing stable primitive values
DUsing useContext hook
✗ Incorrect
Passing new object or function references causes consumers to re-render even if data is unchanged.
When is it better to use props instead of Context?
AWhen data is only needed by a few components nearby
BWhen data is global
CWhen data changes frequently
DWhen data is needed by many nested components
✗ Incorrect
Props are simpler and better when data is only needed by nearby components.
What naming convention is recommended for Context providers?
AUse generic names like Provider
BUse lowercase names
CUse descriptive names like ThemeProvider
DUse numbers in names
✗ Incorrect
Descriptive names improve code readability and understanding.
Explain why and how you would split React Context into smaller parts.
Think about performance and clarity.
You got /4 concepts.
Describe the difference between using props and Context for passing data in React.
Consider component depth and data scope.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of React Context?
easy
A. To handle routing between pages
B. To style components globally
C. To manage component local state only
D. To share data between components without passing props manually
Solution
Step 1: Understand React Context role
React Context allows sharing data across many components without passing props at every level.
Step 2: Compare with other options
Styling, local state, and routing are handled by other React features or libraries, not Context.
Final Answer:
To share data between components without passing props manually -> Option D
Quick Check:
Context shares data without prop drilling [OK]
Hint: Context shares data without passing props repeatedly [OK]
Common Mistakes:
Confusing Context with styling or routing
Thinking Context manages only local state
Assuming Context replaces all state management
2. Which of the following is the correct way to create a Context in React?
easy
A. const MyContext = new Context();
B. const MyContext = createContext();
C. const MyContext = React.createContext();
D. const MyContext = React.useContext();
Solution
Step 1: Recall Context creation syntax
React Context is created using React.createContext() function.
Step 2: Check other options
A uses invalid constructor, B misses React prefix, D calls a hook instead of creating context.
Final Answer:
const MyContext = React.createContext(); -> Option C
Quick Check:
Use React.createContext() to create context [OK]
Hint: Create context with React.createContext() [OK]
Common Mistakes:
Omitting React prefix
Using new keyword incorrectly
Confusing useContext hook with createContext
3. Given this code snippet, what will be logged to the console?