I have a requirement not to execute the code inside useEffect() when the component renders for the first time. For that, I defined a variable outside of the component function and set it to true. And then I checked if the variable is true inside useEffect() hook and if it was true, I set the variable to false and set it to return as shown below. But the code is not working as I expected. The code inside useEffect() executes regardless.
import { useEffect, useState } from 'react'; let isInitial = true; function App() { const [message, setMessage] = useState('First'); useEffect(() => { if (isInitial) { isInitial = false; return; } setMessage('Executed'); }, []); return <p>{message}</p>; } export default App; I wanted to print 'First' inside the <p>. But the result was 'Executed' inside <p> which is not what I expected.