Home  >  Q&A  >  body text

Why is useEffect called twice?

<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>
P粉294954447P粉294954447422 days ago653

reply all(1)I'll reply

  • P粉005417748

    P粉0054177482023-08-25 14:11:23

    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.

    reply
    0
  • Cancelreply