useEffect can take 2 arguments, the first is a function and the second one is an array.
the function is being called as soon as the component gets mounted, but the extended feature that useEffect has is that it has a kind of sensivity list, which lets it run again in case of any argument in the second array changes. making it componentDidUpdate too.
but the console.log() without useEffect runs the way it runs. you don't have any control to it.
this is a simple example:
useEffect(() => {
console.log('count=', count);
}, [count]); // Only re-run the effect if count changes
It also has a cleanUp method, you can (for instance) write a time interval and it could be cleared when the component get unmounted.
you can add a function named 'return' in the useEffect and it's done!
let timer;
useEffect(()=>{
timer = setInterval(() => console.log('hello!'), 1000);
return()=>clearImmediate(timer);
})
Now the interval gets cleared when the component gets unmounted.
Now if we pass something in the array to make useEffect listen to their change, the component will do the cleanUp method when useEffect runs again with the new value.
this is an example:
let timer;
useEffect(()=>{
timer = setInterval(() => console.log('do you need something Mr./Mrs. ' + someOne + ' ?'), 1000);
return()=>clearImmediate(timer);
},[someOne]);
We made a timer which asks someOne if he/she needs something every second.
Now if the 'someOne' changes, it stops asking the previous one and starts to ask the new 'someOne' every second.
by the way, you can name the function 'return' anything you want, or you can just pass an arrow function.