38

Once I run the below code, I get the following error:

React Hook useEffect has a missing dependency: 'list'. Either include it or remove the dependency array react-hooks/exhaustive-deps

I cannot find the reason for the warning.

import React, { useState, useEffect } from 'react';
import axios from 'axios';
import Form from './Form';

const App = () => {
  const [term, setTerm] = useState('pizza');
  const [list, setList] = useState([]);

  const submitSearch = e => {
    e.preventDefault();
    setTerm(e.target.elements.receiptName.value);
  };

  useEffect(() => {
    (async term => {
      const api_url = 'https://www.food2fork.com/api';
      const api_key = '<MY API KEY>';

      const response = await axios.get(
        `${api_url}/search?key=${api_key}&q=${term}&count=5`
      );

      setList(response.data.recipes);
      console.log(list);
    })(term);
  }, [term]);

  return (
    <div className="App">
      <header className="App-header">
        <h1 className="App-title">Recipe Search</h1>
      </header>
      <Form submitSearch={submitSearch} />
      {term}
    </div>
  );
};

export default App;

5 Answers 5

54

Inside your useEffect you are logging list:

console.log(list);

So you either need to remove the above line, or add list to the useEffect dependencies at the end. so change this line

}, [term]);

to

}, [term, list]);
Sign up to request clarification or add additional context in comments.

2 Comments

Is it a bad practice to have variables inside useEffect but not adding them in the dependencies array? For example when you use empty dependencies array to simulate componentDidMount
Well that depends a bit. From the official documentation reactjs.org/docs/hooks-effect.html adding an empty array is telling React that the hook is not dependent on any props or state changes. So you can leave them out if you are a 100% sure that the values of those variables won't change later on (for instance because they are not derived from state or a prop), or when you simply don't care for those changes in the hook that you write.
33

You can disable the lint warning by writing:

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [term]);

1 Comment

This didn't disable the warning.
15

You can disable this by inserting a comment

// eslint-disable-next-line

3 Comments

This didn't disable the warning.
Worked for me. The comment needs to be added right above the line with the empty dependency list.
@AsmaRahimAliJafri for both comments... this will definitely disable the warning. and @all: I NEVER EVER will approve a disable-next-line in a review. It means "I don't care about what my code is doing, so I resign". That's not how to write code. This rule only exists to ...good question... ah: if you're generating code (e.g. AST) and generate random warnings ^^ can't think of any useful scenario otherwise.
10

The dependency array - it's the second optional parameter in the useEffect function. React will recall function defined in the first parameter of useEffect function if parameters inside dependency array will changed from the previous render.

Then you doesn't need to place list variable inside dependency array.

  useEffect(() => {
    // do some

    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [term]);

Comments

5

This warning comes beacuse you have a state 'list' being used inside useEffect. So react warns you that 'list' is not added to dependency so any changes to 'list' state won't trigger this effect to run again.
You can find more help here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.