React Context API: #
Let's imagine you're playing a video game with friends. Instead of shouting instructions across the room, you use walkie-talkies (Context API) to talk directly to anyone who has the same channel!
What We'll Build #
A super simple app that changes background color when you click a button - no prior React knowledge needed!
Step 1: Setup Your Walkie-Talkie (Create Context) #
// 1. Import walkie-talkie parts from Reactimport { createContext } from 'react'; // 2. Create your walkie-talkie channel // (We'll call it "ThemeContext")export const ThemeContext = createContext('light'); // Default channel setting: 'light' mode 🌞
What this means:
createContext()is like buying a new walkie-talkie set'light'is the default channel setting
Step 2: Share Your Walkie-Talkie (Provider) #
// App.jsimport { useState } from 'react';import { ThemeContext } from './ThemeContext';import Room from './Room'; export default function App() { // 3. Create state for current theme (like walkie-talkie volume control) const [theme, setTheme] = useState('light'); // Start with light mode // 4. Toggle function - flips between light/dark const switchTheme = () => { setTheme(theme === 'light' ? 'dark' : 'light'); }; // 5. Share walkie-talkie with everyone in your house (components) return ( <ThemeContext.Provider value={{ theme, switchTheme }}> <Room /> </ThemeContext.Provider> );}
What this means:
useState()is your theme controller (light/dark switch)ThemeContext.Provideris like handing out walkie-talkies to all kids in your housevalue={{ theme, switchTheme }}is the current settings you're broadcasting
Step 3: Listen to the Walkie-Talkie (useContext) #
// Room.jsimport { useContext } from 'react';import { ThemeContext } from './ThemeContext'; export default function Room() { // 6. Tune into the walkie-talkie channel const { theme, switchTheme } = useContext(ThemeContext); // 7. Set room color based on theme const roomStyle = { backgroundColor: theme === 'light' ? 'white' : 'black', color: theme === 'light' ? 'black' : 'white', height: '100vh', padding: '20px' }; return ( <div style={roomStyle}> <h1>My Bedroom</h1> {/* 8. Button to change theme */} <button onClick={switchTheme}> {theme === 'light' ? '🌙 Night Mode' : '☀️ Day Mode'} </button> <p>Current mode: {theme}</p> </div> );}
What this means:
useContext(ThemeContext)is like pressing the "listen" button on your walkie-talkiethemetells you if it's day or night modeswitchThemeis the button that changes modes
How It Works #
- When you click the button:
- It calls
switchTheme() - Which changes the
themevalue in App.js
- It calls
- The walkie-talkie (Context) broadcasts this change
- The Room component hears the change and updates colors
Try It Yourself! #
Create these 3 files:
ThemeContext.js(Step 1)App.js(Step 2)Room.js(Step 3)
Install React if needed:
bash.shnpx create-react-app my-theme-appcd my-theme-appnpm startCopy-paste the code into the right files
Click the button to see magic happen! ✨
Real-Life Comparison #
| Walkie-Talkie | Context API |
|---|---|
| Channel | createContext() |
| Broadcast | Provider |
| Listen | useContext() |
| Settings | value={} |
| Button Press | State change |
Why This Rocks #
- No yelling across rooms (no prop drilling!)
- Direct communication (any component can access context)
- Easy to change settings globally
You just learned React Context! Next step: Try adding a second component that also listens to the theme!


Loading discussion...