I want to use setInterval to trigger my method after every set duration, but when I am using it in react component it is triggering the method twice after set duration.
Example: App.js code
function App() {
setInterval(() => {
console.log("in app");
}, 1000);
}
export default App;
I am getting 2 "in app" message every second instead of once.
The same thing I tried in index.js file
Example: index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
setInterval(() => {
console.log("in index");
}, 1000);
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
In this case I can see "in index" every second in console. (which is what I was expecting in App.js as well)
See attached console image as well.
Is this an expected behavior?
More info:
I created the project using npx create-react-app test and edited App.js file.