This error occurs when you try to call the useContext Hook outside of a React function component or custom Hook. React Hooks have strict rules about where they can be called, and violating these rules leads to this runtime error.
The useContext Hook is part of React's Hooks API, which allows you to use React features like context in function components. Hooks must be called at the top level of a React function component or a custom Hook. Calling useContext outside these valid locations breaks the Rules of Hooks and causes React to throw this error. React enforces this rule because Hooks rely on the component's render cycle to manage state and side effects properly. When you call a Hook outside a component, React cannot associate it with a specific component instance, leading to unpredictable behavior.
Ensure that useContext is called directly inside the body of your function component, before any conditional returns or loops.
// ✅ Correct
import { useContext } from 'react';
function MyComponent() {
const theme = useContext(ThemeContext); // Top level
// ... rest of component
}
// ❌ Incorrect
function MyComponent() {
if (condition) {
const theme = useContext(ThemeContext); // Inside condition
}
}If you need to use context in a reusable way, create a custom Hook that calls useContext. Custom Hooks are allowed to call other Hooks.
// ✅ Create a custom Hook
function useTheme() {
const theme = useContext(ThemeContext);
return theme;
}
// Then use it in your component
function MyComponent() {
const theme = useTheme(); // This works
}Never call useContext inside if statements, for loops, or other conditional blocks. Move the Hook call outside.
// ✅ Correct
function MyComponent({ shouldUseTheme }) {
const theme = useContext(ThemeContext);
const className = shouldUseTheme ? theme.className : 'default';
// ...
}
// ❌ Incorrect
function MyComponent({ shouldUseTheme }) {
if (shouldUseTheme) {
const theme = useContext(ThemeContext); // Inside condition
}
}If you are trying to use useContext in a class component, you need to convert it to a function component or use the Context.Consumer component instead.
// ✅ Using Context.Consumer in a class component
class MyClassComponent extends React.Component {
render() {
return (
<ThemeContext.Consumer>
{theme => <div style={{ color: theme.color }}>Hello</div>}
</ThemeContext.Consumer>
);
}
}
// ✅ Convert to function component
function MyFunctionComponent() {
const theme = useContext(ThemeContext);
return <div style={{ color: theme.color }}>Hello</div>;
}Hooks cannot be called inside event handlers, promises, or regular JavaScript functions. Extract the context value at the top level and use it inside handlers.
// ✅ Correct
function MyComponent() {
const theme = useContext(ThemeContext); // Top level
const handleClick = () => {
console.log('Current theme:', theme); // Using theme, not calling useContext
};
return <button onClick={handleClick}>Click</button>;
}
// ❌ Incorrect
function MyComponent() {
const handleClick = () => {
const theme = useContext(ThemeContext); // Inside event handler
console.log(theme);
};
}React's Rules of Hooks are enforced by the eslint-plugin-react-hooks plugin, which can catch these errors during development. The plugin is included by default in Create React App.
Under the hood, React associates Hook calls with a specific component instance by maintaining a "dispatcher" during rendering. When you call a Hook outside a component, there is no active dispatcher, leading to the error.
If you encounter this error in a codebase that mixes function and class components, consider creating a higher-order component (HOC) or using the Context.Consumer pattern for class components while gradually migrating to function components.
Remember that custom Hooks are the primary mechanism for reusing Hook-based logic. If you find yourself needing context in multiple places, creating a custom Hook like useTheme() or useAuth() can make your code cleaner and more maintainable.
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