Programming

React Hook useState is called in function app which is neither a React function component or a custom React Hook function

25 September 2026 · 5 min read

React Hook useState is called in function app which is neither a React function component or a custom React Hook function

React developers often encounter the cryptic error: “Invalid hook call. Hooks can only be called inside of the body of a function component.” This frustrating message usually pops up when the useState Hook, a fundamental tool for managing state in React, is used incorrectly. Specifically, it appears when useState is called within a function that’s neither a React functional component nor a custom Hook. Understanding why this happens is crucial for building robust and error-free React applications.

Why useState Must Live Inside Components or Hooks

React Hooks, including useState, rely on internal mechanisms tied to the component lifecycle. They need to be called in a predictable order during rendering and updates. A regular JavaScript function lacks this structured lifecycle, making it impossible for React to manage the state updates introduced by Hooks. Attempting to use useState outside this context disrupts the internal state management and leads to unpredictable behavior.

Think of it like trying to plant a seed on concrete. Hooks need the fertile ground of a React component to function correctly. Regular functions simply don’t provide the necessary environment.

Identifying the Culprit

Pinpointing the source of the error can sometimes be tricky. The error message itself often points to the line where useState is called, but the real problem might lie in how that function is being used. Common scenarios include calling useState inside event handlers, helper functions nested within a component, or even within class components. Look closely at how your function interacts with the component structure.

Common Mistakes

  • Calling useState within an event handler directly passed to a DOM element.
  • Using useState inside utility functions that are not custom Hooks.

Moving useState to the Right Place

The solution is usually straightforward: move the logic using useState into a functional component or create a custom Hook. If your logic is within an event handler, refactor it into a separate function declared within the component and call that function from the handler. For utility functions, consider turning them into custom Hooks, allowing them to leverage other Hooks like useState while maintaining a clean separation of concerns.

Refactoring Example

  1. Identify the offending function: Locate where useState is being called incorrectly.
  2. Create a functional component or custom Hook: Wrap the logic in a new function starting with a capital letter (for functional components) or prefixed with “use” (for custom Hooks).
  3. Move the logic: Transfer the code using useState into this new function.
  4. Integrate the new component/Hook: Use your newly created component or call your custom Hook within your existing components.

Benefits of Correct useState Usage

Correctly using useState ensures predictable state management and avoids unexpected errors. It promotes cleaner code, better organization, and improved maintainability. By adhering to React’s rules for Hooks, you harness the full power of its state management system and build more robust applications.

By following these guidelines, you can leverage the power of useState effectively and create more predictable and maintainable React applications. Remember, keeping your useState calls within functional components or custom Hooks is essential for a smooth development experience.

Real-World Example

Imagine building a counter component. Incorrectly placing useState outside the component function will break the counter functionality. Refactoring to place useState within the component ensures that the counter works as expected.

FAQ

Q: Can I use useState in a class component?

A: No, Hooks, including useState, are designed specifically for functional components and custom Hooks.

By understanding the limitations and requirements of React Hooks, you can avoid common pitfalls and write cleaner, more maintainable code. This not only enhances the stability of your application but also improves your development workflow. Explore resources like the official React documentation and community forums to deepen your understanding of Hooks and best practices. Need a refresher on core React concepts? Check out this helpful guide. Further information on Hooks can be found on React’s official documentation on Hooks rules and W3Schools’ React Hooks tutorial. Dive deeper into the intricacies of state management with this in-depth look at useState. Mastering these principles will empower you to build robust and efficient React applications. Remember, a solid understanding of fundamental concepts is key to becoming a proficient React developer.

Question & Answer :
I’m trying to use react hooks for a simple problem

const [personState,setPersonState] = useState({ DefinedObject }); 

with following dependencies.

"dependencies": { "react": "^16.8.6", "react-dom": "^16.8.6", "react-scripts": "3.0.0" } 

but I’m still getting the following error:

./src/App.js

Line 7:
React Hook “useState” is called in function “app” which is neither a React function component or a custom React Hook function react-hooks/rules-of-hooks

Line 39:
‘state’ is not defined
no-undef

Search for the keywords to learn more about each error.

Component code is below:

import React, {useState} from 'react'; import './App.css'; import Person from './Person/Person'; const app = props => { const [personState, setPersonSate] = useState({ person:[ {name:'bishnu',age:'32'}, {name:'rasmi',age:'27'}, {name:'fretbox',age:'4'} ], }); return ( <div className="App"> <h2>This is react</h2> <Person name={personState.person[1].name} age="27"></Person> <Person name={personState.person[2].name} age="4"></Person> </div> ); }; export default app; 

Person component

import React from 'react'; const person = props => { return( <div> <h3>i am {props.name}</h3> <p>i am {props.age} years old</p> <p>{props.children}</p> </div> ) }; export default person; 

Try to capitalize ‘app’ like

const App = props => {...} export default App; 

In React, components need to be capitalized, and custom hooks need to start with use.