How to fix "React Hooks must be called in a React function component or a custom React hook function" error
React decides whether a function is a component or a custom Hook by its name.
React Hooks changed the way we write components in React, giving us a concise way to manage state and side effects in function components.
However, Hooks come with their own set of rules. One common error that React developers run into when they break those rules is "React Hooks must be called in a React function component or a custom React Hook function".
In this article, you'll learn why this error occurs, the four mistakes that usually cause it, and how to fix each one. The examples are tested with React 19 and the current ESLint plugin for React Hooks.
What causes the "React Hooks must be called in a React function component or a custom React Hook function" error#
This error comes from ESLint, specifically the rules-of-hooks rule in the official eslint-plugin-react-hooks package. Projects created with Vite, Next.js, and other React tooling usually include it, so you see the error in your editor or terminal before you run your app.
The rule enforces the Rules of Hooks. Hooks such as useState, useEffect, and useContext:
- Can only be called from React function components or from custom Hooks.
- Must be called at the top level of those functions, not inside loops, conditions, or nested functions.
These rules exist because React keeps track of Hooks by the order in which they're called during each render. If a Hook runs in a regular function, or only some of the time, React can't match each Hook to its state.
React also decides whether a function is a component or a custom Hook by its name. A component name must start with an uppercase letter, like Counter, and a custom Hook name must start with use, like useWindowWidth. That's why the error appears in two forms. When you call a Hook in a class component, ESLint reports the exact message from the title:
React Hook "useState" cannot be called in a class component. React Hooks must be called in a React function component or a custom React Hook function react-hooks/rules-of-hooksWhen you call a Hook in a function that ESLint doesn't recognize as a component or a Hook, it reports a longer version that names the function:
React Hook "useState" is called in function "counter" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter. React Hook names must start with the word "use" react-hooks/rules-of-hooksIf your project doesn't use the ESLint plugin, you get the problem at runtime instead. React throws this error when the component renders:
Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app4 ways to fix the "React Hooks must be called in a React function component or a custom React hook function" error#
The fix depends on where you called the Hook. Let's go through the four most common causes.
1. Start component names with an uppercase letter#
This is the most common cause, and the easiest to miss. In the following example, the component is called counter, with a lowercase c:
import { useState } from "react"
function counter() {
const [count, setCount] = useState(0)
return <button onClick={() => setCount(count + 1)}>Clicked {count} times</button>
}
export default counterThe code looks like a component, but because its name starts with a lowercase letter, ESLint treats it as a regular function and reports the error. To fix it, rename the component so it starts with an uppercase letter:
import { useState } from "react"
function Counter() {
const [count, setCount] = useState(0)
return <button onClick={() => setCount(count + 1)}>Clicked {count} times</button>
}
export default CounterUppercase names matter for more than ESLint. In JSX, React treats <counter /> as an HTML tag and <Counter /> as your component, so a lowercase component name doesn't render as a component either.
2. Name custom Hooks with the use prefix#
When you move Hook logic into a separate function so you can reuse it, that function is a custom Hook, and its name must start with use. The following function tracks the browser window's width, but it's named getWindowWidth:
import { useState, useEffect } from "react"
function getWindowWidth() {
const [width, setWidth] = useState(window.innerWidth)
useEffect(() => {
const onResize = () => setWidth(window.innerWidth)
window.addEventListener("resize", onResize)
return () => window.removeEventListener("resize", onResize)
}, [])
return width
}
export default getWindowWidthESLint reports the error for both useState and useEffect, because it can't tell that getWindowWidth is meant to be a Hook. Rename it to useWindowWidth:
import { useState, useEffect } from "react"
function useWindowWidth() {
const [width, setWidth] = useState(window.innerWidth)
useEffect(() => {
const onResize = () => setWidth(window.innerWidth)
window.addEventListener("resize", onResize)
return () => window.removeEventListener("resize", onResize)
}, [])
return width
}
export default useWindowWidthYou can now call useWindowWidth() at the top level of any component, just like a built-in Hook.
3. Never call React Hooks within a nested function#
All Hooks must be called at the top level of a component or custom Hook. In the following example, useState is called inside the handleClick event handler:
import { useState } from "react"
function Counter() {
function handleClick() {
const [count, setCount] = useState(0)
setCount(count + 1)
}
return <button onClick={handleClick}>Increment</button>
}
export default CounterESLint reports the error and names the nested function:
React Hook "useState" is called in function "handleClick" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter. React Hook names must start with the word "use" react-hooks/rules-of-hooksEven without the error, this code couldn't work: a new state would be created on every click instead of being kept between renders. Move the Hook to the top level of the component, and use its setter function inside the event handler:
import { useState } from "react"
function Counter() {
const [count, setCount] = useState(0)
function handleClick() {
setCount(count + 1)
}
return <button onClick={handleClick}>Clicked {count} times</button>
}
export default CounterThe same applies to callbacks you pass to other Hooks. For example, you can't call useState inside a useEffect callback.
A related mistake is calling a Hook after an early return, or inside an if statement or a loop. ESLint reports that with a different message, React Hook "useState" is called conditionally, and the fix is the same: move the Hook above the condition, so it runs on every render.
4. Never call React Hooks in class components#
Hooks only work in function components and custom Hooks. If you call one in a class component, you get the exact error message from the title:
import React, { useState } from "react"
class Counter extends React.Component {
render() {
const [count, setCount] = useState(0)
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>Click me</button>
</div>
)
}
}
export default CounterThere are three ways to fix this, depending on how much of the component you want to change.
A. Convert the class component to a function component
The cleanest fix is to rewrite the class as a function component, which is what React recommends for new code:
import { useState } from "react"
function Counter() {
const [count, setCount] = useState(0)
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>Click me</button>
</div>
)
}
export default CounterB. Use a higher-order component (HOC)
If you can't rewrite the class right away, you can wrap it in a higher-order component (HOC). An HOC is a function that takes a component and returns a new component. Here, the new component is a function component that calls the Hook and passes its values to the class as props:
import React, { useState } from "react"
function withCounter(WrappedComponent) {
return function WithCounter(props) {
const [count, setCount] = useState(0)
return <WrappedComponent count={count} setCount={setCount} {...props} />
}
}
class Counter extends React.Component {
render() {
const { count, setCount } = this.props
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
)
}
}
export default withCounter(Counter)The Hook still runs in a function component, so it follows the rules, and the class receives the state through props.
C. Use state in the class component
If you want to keep the component as a class, use the class component's own state instead of the useState Hook:
import React from "react"
class Counter extends React.Component {
state = { count: 0 }
handleIncrement = () => {
this.setState((prev) => ({ count: prev.count + 1 }))
}
render() {
return (
<div>
<p>Count: {this.state.count}</p>
<button onClick={this.handleIncrement}>Increment</button>
</div>
)
}
}
export default CounterPassing a function to setState uses the latest state, which avoids bugs when React batches several updates together.
What if the code follows the rules and you still get "Invalid hook call"?#
If ESLint reports no errors but React still throws "Invalid hook call" at runtime, your code probably isn't the problem. The other two causes from React's error message are:
- Mismatched versions of
reactandreact-dom: Make sure both packages in yourpackage.jsonuse the same version. - More than one copy of React: This often happens with a linked local package or a library that bundles its own React. Run
npm ls reactto see every copy installed in your project. If there's more than one, make sure the library lists React as a peer dependency instead of a regular dependency.
Summary#
In this article, you learned what causes the "React Hooks must be called in a React function component or a custom React Hook function" error and how to fix it. Most of the time, the fix is one of these:
- Start component names with an uppercase letter.
- Start custom Hook names with
use. - Call Hooks at the top level of a component or custom Hook, never inside nested functions, conditions, or loops.
- Don't call Hooks in class components. Convert them to function components, wrap them in an HOC, or use class state instead.
Keep the eslint-plugin-react-hooks plugin turned on in your project, so it catches these mistakes as you type.
Looking for the ideal hosting solution for your React applications? You can deploy them for free with Sevalla's Static Site Hosting, or use Application Hosting if your app needs a server.