React useEffect Hooks with setTimeout

0 votes

I am so confused here with React useEffect Hooks with setTimeout. I have exactly the same code as w3school https://www.w3schools.com/react/showreact.asp?filename=demo2_react_useeffect_settimeout2

but my code is rendering "I've rendered 2 times" instead of "I've rendered 1 time". And I have tried it in many code editors, and mine always shows 2..

My: enter image description here

And the only way I can get it to render 1 is refactoring to this instead

useEffect(() => {
     setTimeout(() => {
     setCount(count + 1);
   }, 1000);
 }, []); 

Could someone please explain to me why? Thanks for your help!

I am expecting my code to render the same result as the w3school example.

Jan 27, 2023 in Others by Tejashwini
• 3,820 points
616 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Others

0 votes
1 answer

Next seo test with react testing library

 you need to mock next/head, pass document.head to the container ...READ MORE

answered Feb 11, 2022 in Others by narikkadan
• 63,420 points
1,736 views
0 votes
1 answer

Parsing a string with GetOpt::Long::GetOptions

Hey, have a look at the section ...READ MORE

answered Nov 15, 2018 in Others by nirvana
• 3,130 points
1,304 views
0 votes
1 answer

How to communicate gRPC example working with Node.js?

hi.. you can read below link, it ...READ MORE

answered Dec 2, 2019 in Others by Narendra Reddy Dalli
2,130 views
0 votes
3 answers

Error while joining cluster with node

Hi Kalgi after following above steps it ...READ MORE

answered Jan 17, 2019 in Others by anonymous
14,625 views
0 votes
1 answer

Using SharePoint designer with SharePoint Online ?

Sharepoint gives you a trial account for ...READ MORE

answered Feb 12, 2019 in Others by Priyaj
• 58,090 points
459 views
0 votes
1 answer

Execute a shell script with php

One of the possible reason for this ...READ MORE

answered Feb 24, 2019 in Others by Omkar
• 69,210 points
8,714 views
0 votes
1 answer

How to use useState hook in React with typescript correctly?

You can set a string type for it Explicit way: const ...READ MORE

answered Aug 3, 2022 in TypeSript by Abhinaya
• 1,160 points
3,502 views
0 votes
1 answer

How can I remove a port from url for node app using nginx

If you run your node server on ...READ MORE

answered Apr 10, 2018 in DevOps on Cloud by ajs3033
• 7,300 points
3,670 views
0 votes
4 answers

ReactJS vs Angular Comparison: Which is better?

Parameters React Angular Type React is a JavaScript library, and it ...READ MORE

answered Jan 7, 2021 in Events & Trending Topics by Focusteck
• 140 points
1,203 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP