0
0
Reactframework~30 mins

Context provider in React - Mini Project: Build & Apply

Choose your learning style9 modes available
Using a Context Provider in React
📖 Scenario: You are building a simple React app that shares a theme color across multiple components without passing props manually.
🎯 Goal: Create a React context provider to share a theme color string with child components.
📋 What You'll Learn
Create a React context with a default value
Create a provider component that uses the context
Use the provider to wrap child components
Consume the context value inside a child component
💡 Why This Matters
🌍 Real World
Context providers help share data like themes, user info, or language settings across many components without passing props manually.
💼 Career
Understanding React context is essential for building scalable React apps and working on modern frontend projects.
Progress0 / 4 steps
1
Create a React context with default value
Create a React context called ThemeContext with the default value 'light'.
React
Need a hint?

Use React.createContext and pass the string 'light' as the default value.

2
Create a ThemeProvider component
Create a functional component called ThemeProvider that returns ThemeContext.Provider wrapping children and passes the value 'dark' to the provider.
React
Need a hint?

Use a function component with { children } props and return ThemeContext.Provider with value="dark".

3
Use ThemeProvider to wrap App component
Create a functional component called App that returns ThemeProvider wrapping a Content component.
React
Need a hint?

Wrap the Content component inside ThemeProvider in the App component.

4
Consume context value inside Content component
Inside the Content component, use React.useContext with ThemeContext to get the theme value, then return a div that displays the text Current theme: followed by the theme value.
React
Need a hint?

Call React.useContext(ThemeContext) to get the theme value and display it inside a div.