1

Getting this:

-React Hook useEffect has a missing dependency: 'getData'.
Either include it or remove the dependency array

useEffect(() => {
    setInterval(() => {
        getData();
    }, 300);
}, []);

and in getData calling the api

const getData =useCallback( () => {
    axios.get(`${API_ENDPOINT}/api`,
        {
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
                'Access-control-allow-origin': '*'
            },
            auth: {
                username: 'admin',
                password: 'password'
            }
        }).then(response => {
            setIsLoading(true);
            setPlaylistData(response.data.Clips);
           
        }).catch(error => {
            console.log("Error In GET Data", error);
        });

},[setPlaylistData])
0stone0
  • 34,288
  • 4
  • 39
  • 64
  • 1
    Does this answer your question? [How to fix missing dependency warning when using useEffect React Hook](https://stackoverflow.com/questions/55840294/how-to-fix-missing-dependency-warning-when-using-useeffect-react-hook) – 0stone0 Mar 17 '22 at 10:57
  • 1
    it's pretty clear from the warning. either remove the dependency array`[]` or add `[getData]` – Pranay Nailwal Mar 17 '22 at 10:58

2 Answers2

1

You should add getData to the dependency array, like the warning tells you. Alternatively, you can define getData inside the useEffect so that it is not a dependency anymore.

You should also make sure the effect cleans up after itself by returning a function that cancels the setInterval.

Guillaume Brunerie
  • 4,676
  • 3
  • 24
  • 32
0

We can use a ref to keep track of whether some code has run or not. If not, then run it. If it has, then skip it.

PakDragoon
  • 149
  • 2
  • 11
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 18 '22 at 13:54