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

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:


  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:


  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:


  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.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe