Skip to content
the poor coder

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

Poor Man
Published
Reading time
2 min read
On this page

Understanding the Error: "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"#

As a developer who has worked with React, you may have come across the above error message while using the 'useState' hook. This error message is displayed when you try to use the 'useState' hook outside of a React function component or a custom React Hook function.

The 'useState' hook is a way to manage state in a React component. It allows you to declare a state variable and provide a function to update that variable. The error message is displayed because the 'useState' hook can only be used in a specific context.

React Function Component#

A React function component is a simple function that returns JSX. It is the most common way to create a React component. You can use the 'useState' hook inside a React function component like this:

JavaScript

  import React, { useState } from 'react';

  function App() {
    const [count, setCount] = useState(0);
    return (
      <div>
        <p>You clicked {count} times</p>
        <button onClick={() => setCount(count + 1)}>
          Click me
        </button>
      </div>
    );
  }
  

In this example, we're using the 'useState' hook inside the 'App' function component to manage the state of a counter. Whenever the button is clicked, the 'setCount' function updates the 'count' variable, which causes the component to re-render and display the updated count value.

Custom React Hook Function#

A custom React Hook function is a function that starts with the word 'use' and can call other hooks. This function can be used to encapsulate complex logic and state management. You can use the 'useState' hook inside a custom React Hook function like this:

JavaScript

  import { useState } from 'react';

  function useCounter(initialCount) {
    const [count, setCount] = useState(initialCount);
    const increment = () => setCount(count + 1);
    return [count, increment];
  }
  

In this example, we're creating a custom React Hook function called 'useCounter'. This function uses the 'useState' hook to manage a counter state variable and returns an array with the current count value and an 'increment' function. This custom hook can then be used in a React function component like this:

JavaScript

  import React from 'react';
  import useCounter from './useCounter';

  function App() {
    const [count, increment] = useCounter(0);
    return (
      <div>
        <p>You clicked {count} times</p>
        <button onClick={increment}>
          Click me
        </button>
      </div>
    );
  }
  

In this example, we're using the 'useCounter' custom hook inside the 'App' function component to manage the state of a counter. Whenever the button is clicked, the 'increment' function updates the count value, which causes the component to re-render and display the updated count value.

Remember to always use the 'useState' hook inside a React function component or a custom React Hook function to avoid the error message.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC