Back to Roadmap
7:00

React Context API

Learn how to manage global state in React using the Context API without external libraries

7 MIN READ VERIFIED CURRICULUM

The React Context API is a built-in feature that allows you to share data across multiple components without manually passing props at every level.

It helps solve the problem of prop drilling, where data has to be passed through many intermediate components.

What is Context API?

Context API is a mechanism in React that provides a way to pass data globally through the component tree.

It allows components to consume shared state without explicitly receiving it through props.

Why Use Context API?

As applications grow, passing props through multiple levels becomes difficult and hard to maintain.

Context API simplifies this by providing a centralized way to manage shared state.

Creating a Context

You create a context using the createContext function provided by React.

import { createContext } from 'react';

const UserContext = createContext();
javascript

Providing Context

The Provider component is used to wrap parts of the application where you want to share data.

It makes the context value available to all child components.

import { UserContext } from './UserContext';

function App() {
  return (
    <UserContext.Provider value={{ name: 'John' }}>
      <Profile />
    </UserContext.Provider>
  );
}
javascript

Consuming Context

You can access context values using the useContext hook inside functional components.

import { useContext } from 'react';
import { UserContext } from './UserContext';

function Profile() {
  const user = useContext(UserContext);

  return <h1>Hello, {user.name}</h1>;
}
javascript

How Context API Works

Context API works by creating a shared state that can be accessed by any component within its provider scope.

When the value changes, all consuming components automatically re-render.

Avoiding Prop Drilling

Prop drilling occurs when data is passed through many layers of components unnecessarily.

Context API eliminates this by providing direct access to shared data.

Updating Context State

Context can also manage state by combining it with useState or useReducer.

const UserContext = createContext();

function UserProvider({ children }) {
  const [user, setUser] = useState(null);

  return (
    <UserContext.Provider value={{ user, setUser }}>
      {children}
    </UserContext.Provider>
  );
}
javascript

Context vs Props

Props are used for passing data between parent and child components, while context provides global access.

Context is best for global state like authentication, themes, and user settings.

Performance Considerations

Excessive use of context can cause unnecessary re-renders if not structured properly.

Splitting context into smaller providers can help improve performance.

Real-World Usage

Context API is commonly used for authentication, theme switching, language settings, and global UI state.

It is widely used in React applications where lightweight global state management is needed.

Best Practices

Keep contexts focused and avoid putting all application state into a single context.

Combine Context API with useReducer for more complex state logic.

Summary

React Context API provides a simple way to manage global state without external libraries.

Understanding context is essential for building scalable and maintainable React applications.