Skip to content

How to Fix React createContext Usage

DodaTech Updated 2026-06-26 1 min read

The Problem

In this quick-fix, you'll learn how to create and use React Context correctly. This matters because wrong context usage leads to undefined values and missing provider errors. In real-world applications, this pattern appears in theming internationalization user authentication state.

The Wrong Way

const ThemeContext = React.createContext();
function App() { return <Child />; }
function Child() {
  const theme = useContext(ThemeContext);
  return <div>{theme}</div>;
}
theme is undefined no Provider wrapping App

The Right Way

const ThemeContext = React.createContext("light");
function App() {
  return <ThemeContext.Provider value="dark"><Child /></ThemeContext.Provider>;
}
function Child() {
  const theme = useContext(ThemeContext);
  return <div>{theme}</div>;
}
theme is dark Provider supplies the value

Prevention

  • Always wrap consuming components with a Provider
  • Provide sensible default to createContext
  • Place Provider high enough in tree
  • Use useContext for consuming context

FAQ

### What happens if I consume context without a Provider??

Component receives default value passed to createContext. If no default provided returns undefined.

Should I put everything in one context??

No. Split by domain and update frequency. Combining unrelated values causes unnecessary re-renders.

How deep should I place the Provider??

Highest level where all consumers can access it. Usually app root or feature root.

This guide is brought to you by DodaTech -- built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro