Why is useEffect called twice?
P粉294954447
P粉294954447 2023-08-24 16:08:22
0
1
654
<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粉294954447
P粉294954447

reply all(1)
P粉005417748

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!