This error occurs when a React Context Provider component is rendered without the required "value" prop. While React may not always throw this error explicitly, omitting the value prop causes the provider to pass undefined to consumers, leading to unexpected behavior and runtime errors.
This error indicates that a Context.Provider component is missing its required "value" prop. In React's Context API, the Provider component must receive a "value" prop that will be passed down to all consuming components in the tree below it. When you create a context using React.createContext(), you get a Provider component that requires a value prop to function correctly. If you render the Provider without specifying a value, React will pass undefined to all consumers, which often leads to errors when those consumers try to access properties or methods on the context value. This is particularly problematic because React doesn't always throw a clear error when the value prop is missing. Instead, consumers receive undefined and may fail silently or with obscure errors like "Cannot read property 'x' of undefined" when they try to use the context.
Ensure your Provider component includes the value prop with your context data:
import { createContext } from 'react';
const MyContext = createContext();
function MyProvider({ children }) {
// Define the value to be shared
const contextValue = {
user: 'John',
isLoggedIn: true
};
// CORRECT: Provider includes value prop
return (
<MyContext.Provider value={contextValue}>
{children}
</MyContext.Provider>
);
}Make sure you're using "value" (not "values" or any other variant).
If your value is an object or array, wrap it in useMemo to prevent unnecessary re-renders:
import { createContext, useMemo } from 'react';
const MyContext = createContext();
function MyProvider({ children }) {
const [user, setUser] = useState(null);
// Memoize the context value to maintain stable reference
const contextValue = useMemo(() => ({
user,
setUser
}), [user]);
return (
<MyContext.Provider value={contextValue}>
{children}
</MyContext.Provider>
);
}This prevents all consumers from re-rendering when the Provider re-renders, unless the actual value dependencies change.
Protect your consumers by creating a custom hook that validates the context value:
import { createContext, useContext } from 'react';
const MyContext = createContext(undefined);
// Custom hook that throws if context is undefined
function useMyContext() {
const context = useContext(MyContext);
if (context === undefined) {
throw new Error('useMyContext must be used within a MyProvider');
}
return context;
}
// Usage in components
function MyComponent() {
const { user } = useMyContext(); // Throws clear error if no provider
return <div>{user.name}</div>;
}This pattern makes debugging much easier by failing loudly when the provider is missing.
Ensure the Provider wraps all components that need to consume the context:
// CORRECT: Provider wraps all consumers
function App() {
return (
<MyProvider>
<Header />
<Content />
<Footer />
</MyProvider>
);
}
// INCORRECT: Some consumers outside provider
function App() {
return (
<>
<Header /> {/* Cannot access context */}
<MyProvider>
<Content /> {/* Can access context */}
</MyProvider>
<Footer /> {/* Cannot access context */}
</>
);
}React 19 Simplified Provider Syntax
Starting in React 19, you can render the context itself as a provider without using the .Provider property:
// React 19+
return <MyContext value={contextValue}>{children}</MyContext>;
// React 18 and earlier
return <MyContext.Provider value={contextValue}>{children}</MyContext.Provider>;Default Values and Provider Hierarchy
The default value passed to createContext(defaultValue) is only used when there is NO Provider component in the parent tree. If a Provider exists but has value={undefined}, consumers will receive undefined, NOT the default value. This is a common source of confusion.
TypeScript Integration
When using TypeScript, define your context type explicitly and use undefined as the default to enforce provider checking:
interface MyContextType {
user: User;
login: (user: User) => void;
}
const MyContext = createContext<MyContextType | undefined>(undefined);This forces you to handle the undefined case in your custom hook, making the code more robust.
Performance Considerations
Every time a Provider's value changes reference (even if the content is the same), ALL consuming components re-render. Always use useMemo for object/array values, or consider splitting contexts if different parts of your state change at different frequencies.
React Hook useCallback has a missing dependency: 'variable'. Either include it or remove the dependency array react-hooks/exhaustive-deps
React Hook useCallback has a missing dependency
Cannot use private fields in class components without TS support
Cannot use private fields in class components without TS support
Cannot destructure property 'xxx' of 'undefined'
Cannot destructure property of undefined when accessing props
useNavigate() may be used only in the context of a <Router> component.
useNavigate() may be used only in the context of a Router component
Cannot find module or its corresponding type declarations
How to fix "Cannot find module or type declarations" in Vite