We switched to React 18 from react 17. Strangely all the API code that is running during component mount in useEffect has started executing twice. When we applied console we found that it look like component mounting, then unmount and then remounting. Let me share a code snippet that illustrate this behaviour:
import { useEffect, useState } from "react";
import "./styles.css";
export default function App() {
const [apiCalled, setApiCalled] = useState(0);
const callApi = () => {
setApiCalled((prev) => prev + 1);
};
useEffect(() => {
// Mount component
callApi();
}, []);
return <div className="App">Api Called: {apiCalled}</div>;
}
Here output is:
Api Called: 2
While expected output is:
Api Called: 1
This is working alright in version 17. Please help me know resolution of this problem.