Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Hook "useEffect" is called in function "shoes" which is neither a React function component or a custom React Hook

I'm getting the following error:

React Hook "useEffect" is called in function "shoes" which is neither a React function component or a custom React Hook function react-hooks/rules-of-hooks

I'm not sure exactly what is the error in my code:

import React, {useState, useEffect} from 'react';
import "../App.css"

function shoes() {
    useEffect(() => {
        fetchItems();
    }, []);

    const fetchItems = async () => {
        const data = await fetch('xxxxxxxxxxxxxxxxxxxxx')
        console.log(data)
    };

    return ( < h1 className = "main" > Shoes < /h1> );
    }
}

export default shoes;
like image 515
Joe Avatar asked Mar 23 '20 17:03

Joe


People also ask

Which is neither a React function component or a custom React hook function React Hooks rules of Hooks?

React Hook 'useState' is called in function that is neither a React function component nor a custom React Hook function.

Is useEffect is a React hook?

The useEffect Hook allows you to perform side effects in your components. Some examples of side effects are: fetching data, directly updating the DOM, and timers. useEffect accepts two arguments.

When useEffect hook is called?

The useEffect Hook is called every time the component renders. It works similarly to componentDidMount and componentDidUpdate in React class components.

Can we use useEffect in functional component?

useEffect() is for side-effects. A functional React component uses props and/or state to calculate the output. If the functional component makes calculations that don't target the output value, then these calculations are named side-effects.


1 Answers

As described here, React component names need to start with a capital letter.
That's because React treats components starting with a lowercase letter as DOM tags.

Therefore you should name it Shoes.

like image 135
Arnaud Claudel Avatar answered Oct 13 '22 13:10

Arnaud Claudel