How to fix MyContext.Provider requires a value prop in React
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.
Quick fix
Fixes MyContext.Provider requires a value prop
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>
);
}- Add the value prop to your Context.Provider
- Use useMemo to stabilize object values
- Create a custom hook with undefined checks
- Verify provider placement in component tree
import { createContext } from 'react';const MyContext = createContext();function MyProvider({ children }) {// Define the value to be sharedconst contextValue = {user: 'John',isLoggedIn: true};// CORRECT: Provider includes value propreturn (<MyContext.Provider value={contextValue}>{children}</MyContext.Provider>);}MyContext.Provider requires a value prop
What this error means
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.
Typical symptoms
- Components using useContext receive undefined instead of expected context value
- Errors like "Cannot read property 'x' of undefined" when accessing context properties
- Context consumers fail to render or throw runtime errors
- TypeScript errors about accessing properties of undefined context values
- Application breaks when refactoring context providers
Common causes
- Rendering a Context.Provider component without the required value prop
- Accidentally using "values" or another typo instead of "value" as the prop name
- Forgetting to pass the value prop when creating a custom provider component
- Removing the value prop during refactoring without updating consumer components
- Conditionally rendering providers without handling the no-provider case in consumers
How to fix "MyContext.Provider requires a value prop"
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 */}
</>
);
}Advanced notes
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.
Frequently asked questions
- What does "MyContext.Provider requires a value prop" mean?
- 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. createContext(), you get a Provider component that requires a value prop to function correctly.
- How do I fix "MyContext.Provider requires a value prop"?
- 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.
- What causes "MyContext.Provider requires a value prop"?
- Rendering a Context.Provider component without the required value prop Accidentally using "values" or another typo instead of "value" as the prop name Forgetting to pass the value prop when creating a custom provider component
- Is the "MyContext.Provider requires a value prop" error serious?
- It is a medium-severity error โ more of a blocker than a risk. The steps above resolve it.