Skip to content
Advertisement

How to stop my component from being made twice in development

I am following this tutorial to make a javascript calendar in react

I have a working calendar UI with the following code

JavaScript

However in development, my calendar is made twice

enter image description here

I assume this is because of React.StrictMode in development as useEffect seems to be running twice. If I run npm run build and npm start to mimic production, I only see one calendar.

Is there still a way for my calendar to appear once in development?

Advertisement

Answer

The problem you’re having is that your dependencies in the useEffect are changing on the new render cycle and so showCalendar is triggered multiple times.

To keep your code and only run it once, you should just be able to define the variables:

JavaScript

and remove the dependencies from the useEffect, as they don’t change:

JavaScript

However the useEffect is entirely unecessary, and your approach is strange.

Just do:

JavaScript

or:

JavaScript

Hope that helps.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement