Videos
When do you need to use it? Why do I need to put a empty array even though its empty? When would I put something in the empty array?
Sorry if stupid noob question.
If you use inside your useEffect some functions/variables that are declared outside of the useEffect , then you would have to add it as a dependency in case it might change.
External variables that are used inside of useEffect should be inculded in the dependency array, and your functions are defined outside of it, so that's the reason it's warning you.
Though there are some exceptional cases when you don't have to add everything.
For example, if you call setState method inside useEffect, it doesn't have to be inculded in the dependency array (because React will make sure this function won't change).
Read more about what to add to the dependencies here: https://devtrium.com/posts/dependency-arrays
If your functions or methods are going to be called only for one time then it's better to declare and invoke the function in the same useEffect hook.
For Example:-
useEffect(() => {
const options = {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
};
const getCurrentDate = () => {
let date = new Date();
setDate(date.toLocaleDateString("en-us", options));
};
getCurrentDate();
}, []);
As the above code shows try to include the functions or methods inside your useEffect hook or else if you want to try to add some dependency which can be variable that when that changes your useEffect will trigger again.