How to fix MyContext.Provider requires a value prop in React

ReactBEGINNERMEDIUMUpdated Dec 9, 2025ยท 115 views

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>
  );
}
  1. Add the value prop to your Context.Provider
  2. Use useMemo to stabilize object values
  3. Create a custom hook with undefined checks
  4. Verify provider placement in component tree
react โ€” error
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>
);
}
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

  1. Rendering a Context.Provider component without the required value prop
  2. Accidentally using "values" or another typo instead of "value" as the prop name
  3. Forgetting to pass the value prop when creating a custom provider component
  4. Removing the value prop during refactoring without updating consumer components
  5. Conditionally rendering providers without handling the no-provider case in consumers

How to fix "MyContext.Provider requires a value prop"

1Add the value prop to your Context.Provider

Ensure your Provider component includes the value prop with your context data:

jsx
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).

2Use useMemo to stabilize object values

If your value is an object or array, wrap it in useMemo to prevent unnecessary re-renders:

jsx
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.

3Create a custom hook with undefined checks

Protect your consumers by creating a custom hook that validates the context value:

jsx
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.

4Verify provider placement in component tree

Ensure the Provider wraps all components that need to consume the context:

jsx
// 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:

jsx
// 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:

typescript
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.

Official resources & further reading