Why is useEffect called twice?
P粉294954447
2023-08-24 16:08:22
<p><br /></p>
<pre class="brush:php;toolbar:false;">import { useContext, useEffect, useState } from 'react';
const Log = () => {
useEffect(() => {
console.log('Running ...')
},[])
return(<p>here</p>)
}
export default Log;</pre>
<p>Whenever this code runs, I receive the <strong>Running...</strong> message twice in the browser console. </p>
<p>I think it should only run once because I have an empty second parameter in useEffect. </p>
<p>Can anyone explain why it runs twice? </p>
This is due to <StrictMode> possibly being in your root tree.
What is strict mode?
How does it cause
useEffect()
to run twice?Note:
Strict mode checks only run in development mode; they do not affect production builds.