Late to the party but thought of putting this example here which I did for my own understanding after reading above comments:
import './App.css';
import { useEffect, useState } from 'react';
function App() {
const [name, setName] = useState('John');
useEffect(()=>{
console.log("1- No dependency array at all");
});
useEffect(()=>{
console.log("2- Empty dependency array");
}, []);
useEffect(()=>{
console.log("3- Dependency array with state");
}, [name]);
const clickHandler = () => {
setName('Jane');
}
return (
<div className="App">
<button onClick={clickHandler}>Click to update state</button>
<p>{`Name: ${name}`}</p>
</div>
);
}
export default App;
OUTPUT
On page load
1- No dependency array at all
2- Empty dependency array
3- Dependency array with state
1- No dependency array at all
2- Empty dependency array
3- Dependency array with state
On button click -state update
1- No dependency array at all
3- Dependency array with state
null
? Isn't it just the same as not putting the code in a useEffect hook? – Draughts