Remember our school website example where we used useContext
to share the school name? Now, let's expand on that idea to understand how the Context API helps us pass data through our entire component tree.
useContext
?In our previous lesson, we learned that useContext
is like a loudspeaker that lets us share information with many parts of our React app at once. We used it to share our school name across different components.
The Context API is the bigger system that makes useContext
work. It's like the entire announcement system in our school, not just the loudspeaker. Let's break it down:
First, we need to set up our announcement system. We do this in a separate file:
// SchoolContext.js
import React from 'react';
const SchoolContext = React.createContext();
export default SchoolContext;
This creates our SchoolContext
, which will hold our school information.
Now, we need to use the Provider to make our announcement. We'll do this in our main App component:
// App.js
import React from 'react';
import SchoolContext from './SchoolContext';
import Header from './Header';
import MainContent from './MainContent';
import Footer from './Footer';
function App() {
const schoolInfo = {
name: "Friendly Neighborhood School",
principal: "Ms. Johnson",
founded: 1995
};
return (
<SchoolContext.Provider value={schoolInfo}>
<div className="App">
<Header />
<MainContent />
<Footer />
</div>
</SchoolContext.Provider>
);
}
export default App;
Here's what's new:
SchoolContext.Provider
.schoolInfo
object as the value
prop to the Provider.Now that we've made our announcement, let's see how different parts of our app can listen to it:
// Header.js
import React, { useContext } from 'react';
import SchoolContext from './SchoolContext';
function Header() {
const schoolInfo = useContext(SchoolContext);
return (
<header>
<h1>{schoolInfo.name}</h1>
<p>Principal: {schoolInfo.principal}</p>
</header>
);
}
export default Header;
// MainContent.js
import React, { useContext } from 'react';
import SchoolContext from './SchoolContext';
function MainContent() {
const schoolInfo = useContext(SchoolContext);
return (
<main>
<h2>Welcome to {schoolInfo.name}!</h2>
<p>We've been providing quality education since {schoolInfo.founded}.</p>
</main>
);
}
export default MainContent;
// Footer.js
import React, { useContext } from 'react';
import SchoolContext from './SchoolContext';
function Footer() {
const schoolInfo = useContext(SchoolContext);
return (
<footer>
<p>© {new Date().getFullYear()} {schoolInfo.name}. All rights reserved.</p>
</footer>
);
}
export default Footer;
In each of these components:
useContext
from React and our SchoolContext
.useContext(SchoolContext)
to get the current value of SchoolContext
(our schoolInfo
object).Imagine if we didn't have Context. We'd have to pass our schoolInfo
to Header
, then from Header
to MainContent
, and so on. It might look like this:
<Header schoolInfo={schoolInfo}>
<MainContent schoolInfo={schoolInfo}>
<Footer schoolInfo={schoolInfo} />
</MainContent>
</Header>
This can get messy quickly, especially in larger apps. Context lets us skip all that and make the information available wherever we need it!
Context is great for sharing data that many components need, like:
But remember, with great power comes great responsibility! Don't overuse Context. If you only need to pass information to one or two components, regular props might be simpler.
The Context API, with its createContext
function, Provider
component, and useContext
hook, gives us a powerful way to share information across our React app. It's like setting up an announcement system for our code, making sure every part of our app can easily access the information it needs.
By expanding on our school website example, we've seen how Context allows us to pass complex data (like our schoolInfo
object) through our component tree without having to manually pass props at every level.
Keep practicing with Context in your React projects. Soon, you'll be managing complex data flows with ease!