1

I've got a question regarding a missing dependency warning.

I've got a GET request where I'd like to set the catch error state. However when setting the error it asks to add the setError in the dependency list. The dependency should only be the {value}, not the setError right?

useEffect(() => {
    if (value.length === 3) {
      setLoading(true);
      axios
        .get(
          `https://url.com?search=${value}`
        )
        .then((res) => setCompanies(res.data.data))
        .then(() => setLoading(false))
        .catch((err) => {
          if (err.response.status === 500) {
            setError("something went wrong");
          }
        });
    }
  }, [value]);

Warning notification: React Hook useEffect has a missing dependency: 'setError'. Either include it or remove the dependency array.

fernon
  • 93
  • 6
  • It should answer all of your questions https://stackoverflow.com/questions/55840294/how-to-fix-missing-dependency-warning-when-using-useeffect-react-hook – Sullivan Tobias Jul 28 '22 at 10:00

1 Answers1

0

you need to declare setError() and error :

const [error,setError] = useState('');

useEffect(() => {
    if (value.length === 3) {
      setLoading(true);
      axios
        .get(
          `https://url.com?search=${value}`
        )
        .then((res) => setCompanies(res.data.data))
        .then(() => setLoading(false))
        .catch((err) => {
          if (err.response.status === 500) {
            setError("something went wrong");
          }
        });
    }
  }, [value]);
devseo
  • 1,182
  • 1
  • 13
  • 26