MyInternships.in

React Hooks

React Context API

Context provides a value to an entire subtree without passing props through every level. You create a context, wrap part of the tree in its Provider, and any descendant reads the value with useContext.


What is Context API in React?

Context provides a value to an entire subtree without passing props through every level. You create a context, wrap part of the tree in its Provider, and any descendant reads the value with useContext.

Why Context API matters

Theme, current user, language and cart data are needed all over an app. Threading them through ten intermediate components that do not use them — prop drilling — makes every component harder to change.

Context API example

Create, provide, consume
JSX
// AuthContext.jsx
import { createContext, useContext, useState } from 'react';

const AuthContext = createContext(null);

export function AuthProvider({ children }) {
  const [user, setUser] = useState(null);
  const value = { user, login: setUser, logout: () => setUser(null) };
  return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
}

export function useAuth() {
  const ctx = useContext(AuthContext);
  if (!ctx) throw new Error('useAuth must be used inside AuthProvider');
  return ctx;
}

How this works

Exporting a custom useAuth hook instead of the raw context gives a clear error when a component is used outside the provider, and hides the context object from consumers.

Using it anywhere below the provider

JSX
function Header() {
  const { user, logout } = useAuth();
  return user ? <button onClick={logout}>Sign out</button> : <LoginLink />;
}

Key points to remember

  • Every consumer re-renders when the provider value changes.
  • Memoise the value object with useMemo, or the provider hands out a new object every render.
  • Split unrelated data into separate contexts so a theme change does not re-render cart consumers.
  • Context is a delivery mechanism, not a state manager — it does not batch, cache or persist anything.

Common mistakes with Context API

  • Putting the entire application state in one context, so every change re-renders everything.
  • Passing an inline object as value, defeating any downstream memoisation.
  • Reaching for context when a component only needs to pass a prop one level down.

React Context API— Interview Questions & FAQs

Is Context a replacement for Redux?+

For low-frequency global values — theme, locale, current user — yes. For frequently updated state with many consumers, Redux Toolkit or Zustand offer selective subscriptions and devtools that context alone does not.

Why does everything re-render when my context changes?+

Every consumer re-renders whenever the provider value reference changes. Memoise the value with useMemo and split large contexts into smaller focused ones.

Related React Topics

Keep learning with these closely related lessons.

Ready to use your React skills?

Find verified React internships and fresher developer jobs across India.

Browse React Internships