In this module, you'll learn about React's Context API and how it can be used to share state between components without prop drilling. You'll explore how to create and consume context, and understand when to use it effectively in your applications.
In a typical React application, data is passed top-down (parent to child) via props, but this can be cumbersome for certain types of props that are required by many components within an application, like application state. Context provides a way to share data or state between components without having to explicitly pass a prop through every level of the tree.
The Context API solves the problem of prop drilling, which occurs when you pass data through multiple layers of components just to reach a deeply nested component that needs that data.
We use the Context API when we have:
Because of one-way data flow in React, state must exist above the components that need it. Context helps you centralize application state and make it easily accessible to any component in the tree, no matter how deeply nested.
To provide data with Context, you need to complete these steps:
createContext()
Here's a basic example of creating a context provider:
// context/ThemeContext.js import React, { createContext } from 'react'; // Create the context export const ThemeContext = createContext(); // Create the context provider export const ThemeProvider = ({ children }) => { const theme = { colors: { primary: '#ff5722', background: '#222831', text: '#ffffff' } }; return ( <ThemeContext.Provider value={theme}> {children} </ThemeContext.Provider> ); };
Then, you would wrap your application (or part of it) with the provider:
// index.js or App.js import { ThemeProvider } from './context/ThemeContext'; root.render( <ThemeProvider> <App /> </ThemeProvider> );
Once you've set up a context provider, any component within the provider's tree can access the context data using the useContext
hook.
Here's how to consume context data in a component:
// components/Button.js import React, { useContext } from 'react'; import { ThemeContext } from '../context/ThemeContext'; function Button({ children }) { // Access the context data const theme = useContext(ThemeContext); return ( <button style={{ backgroundColor: theme.colors.primary, color: theme.colors.text }} > {children} </button> ); }
Key benefits of using Context:
Context is perfect for themes, user authentication, language preferences, and other global application states that many components throughout your application need to access.