📖 Scenario: You are building a simple React app that shares a theme color across multiple components without passing props manually.
🎯 Goal: Create a React app that uses Context to provide a theme color to nested components following best practices.
📋 What You'll Learn
Create a context with React.createContext
Provide the context value using a Context.Provider
Consume the context value in a child component using useContext hook
Follow best practices for naming and structure
💡 Why This Matters
🌍 Real World
Sharing global data like themes, user info, or settings across many components without passing props manually.
💼 Career
Understanding React Context is essential for building scalable React apps and is a common requirement in frontend developer roles.
Progress0 / 4 steps
1
Create the Theme Context
Create a context called ThemeContext using React.createContext with the default value 'light'.
React
Hint
Use React.createContext('light') and assign it to ThemeContext.
2
Add Theme Provider Component
Create a functional component called ThemeProvider that returns ThemeContext.Provider with the value 'dark' and renders its children inside.
React
Hint
Wrap {children} inside ThemeContext.Provider with value 'dark'.
3
Consume Theme Context in Child Component
Create a functional component called ThemedButton that uses the useContext hook to get the theme from ThemeContext and renders a button with text showing the current theme.
React
Hint
Use const theme = useContext(ThemeContext) inside ThemedButton and display it inside a button.
4
Use ThemeProvider and ThemedButton in App
Create a functional component called App that uses ThemeProvider to wrap ThemedButton and export App as default.
React
Hint
Wrap ThemedButton inside ThemeProvider in App and export App as default.
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?