0

I am currently using useEffect and I get this error below

"WARNING in src\newComponents\jobSearch\UseFetch.js Line 36:8: React Hook useEffect has a missing dependency: 'fetchData'. Either include it or remove the dependency array react-hooks/exhaustive-deps "

Here is my function

  const fetchData = async () => {
        setIsLoading(true);
        try {
            const response = await axios.request(options);

            setData(response.data.data);
            setIsLoading(false);
        } catch (error) {
            setError(error);
            console.log(error)
        } finally {
            setIsLoading(false);
        }
    };

    useEffect(() => {
        fetchData();
    }, []);

Can you please help me out

7
  • Did you import the useEffect Commented Jul 24, 2024 at 12:17
  • @UdanJayanith yes I did Commented Jul 24, 2024 at 12:23
  • Try removing [ ] in useEffect or pass a value to useEffect dependency list Commented Jul 24, 2024 at 12:28
  • If you want to define the async function outside of the useEffect hook, you have to add it to the dependency list of useEffect and wrap its definition into a useCallback with the necessary dependencies to prevent unnecessary calls. Commented Jul 24, 2024 at 13:11
  • i'm confused as to what is happening here. i'm currently staring at some working code i wrote that has a useEffect with an empty dependency array that calls an async function defined a few lines above it. looks remarkably similar to this, but i'm not getting this warning. what exactly is causing the warning? Commented Jul 24, 2024 at 13:21

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.