An API call is being made from useEffect() and when the application is refreshed, useEffect() is called twice and API is call is made twice.
// App.js
import { useEffect, useCallback } from "react";
function App() {
const fetchUsers = useCallback(async () => {
try {
const response = await fetch(
"https://jsonplaceholder.typicode.com/users"
);
const data = await response.json();
console.log("data: ", data); // displayed two times in console.
} catch (error) {
console.log("Error in catch!");
}
}, []);
useEffect(() => {
fetchUsers();
}, [fetchUsers]);
}
export default App;
how to fix this? useCallback() also has no effect in not invoking fetchUsers() function twice.
Here is the full code. CodeSandBox
using react v18
Edit: Removing <React.StrictMode> has solved the issue, but I don't want to remove it, as it is useful in warning potential issues in the application.
Is there any other solution?